Question

Given the node structure and the head pointer (headptr) of a linked list write a code...

Given the node structure and the head pointer (headptr) of a linked list write a code to

move the last element to the head of the list.

struct node

{

   int value;

   struct node *next;

};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

------------------main.cpp-----------

#include <iostream>
#include <stdlib.h>
using namespace std;

struct node //struct for node of the linked list
{
int value; // stores node value
struct node *next; // pointer to next node of linked list
};

struct node* header = NULL; //head of linked list


void insertBack(int value) //This function inserts new node at the end of the list
{
struct node* new_node = (struct node*) malloc(sizeof(struct node)); //create new node
new_node->value = value;
struct node* ptr = header;
while (ptr != NULL && ptr->next != NULL) //traverse till last element
{
ptr = ptr->next;
}
if(ptr == NULL) //if list was empty, insert first node
{
header = new_node;
new_node->next = NULL;
return;
}
ptr->next = new_node; //insert element at last position
new_node->next = NULL;
}

void LastToHead(struct node* &head) //moves the last element to the head of the list.
{
struct node* ptr = head;
while (ptr != NULL && ptr->next!= NULL && ptr->next->next != NULL) //traverse till second last element
{
ptr = ptr->next;
}
if(ptr == NULL) //if list was empty, then return
{
return;
}
if(ptr->next == NULL) //if list has only one element, then return
{
return;
}
//if list has atleast two nodes, the ptr points to second last node
ptr->next->next = head; //inserts last node into beginning of list
head = ptr->next; //makes head point to the new first node
ptr->next = NULL; //makes previous second last node as last node
}

void DisplayList(struct node* head) //This function displays the list
{
struct node* ptr = head;
cout<<"\nThe list is : ";   
while (ptr != NULL) //traverse nodes one by one and print
{
cout<< ptr->value <<" ------> ";
ptr = ptr->next;
}
cout<< "NULL" << endl;
}


int main()
{
insertBack(2); //Add elements to back of list
insertBack(4);
insertBack(6);
insertBack(14);
insertBack(61);
insertBack(22);
insertBack(16);
DisplayList(header); //display list

LastToHead(header); //inserts last node 16 to beginning of list
DisplayList(header); //display list
return 0;
}

------------------Screenshots main.cpp-----------

-/pp/linked list indented/main.cpp - Sublime Text (UNREGISTERED) ti 1) 9:07 AM * main.cpp Given the node structure and the he/cpp/linked list indented/main.cpp - Sublime Text (UNREGISTERED) ti 1) 9:07 AM * 25 26 27 28 29 30 main.cpp Given the node st/cpp/linked list indented/main.cpp - Sublime Text (UNREGISTERED) ti 1) 9:07 AM * main.cpp х Given the node structure and the

------------------Output-----------

9:06 AM vs@ubuntu:-/pp/linked list indented vs@ubuntu:-/pp/linked list indented$ g++ main.cpp vs@ubuntu:-/cpp/linked list ind

-----------------------------------------

I hope this helps you,

Please rate this answer if it helped you,

Thanks for the opportunity

Add a comment
Know the answer?
Add Answer to:
Given the node structure and the head pointer (headptr) of a linked list write a code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 13. Given the following structure struct node { struct node *next; int id; }; Write a...

    13. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list as the last node of the linked list struct node *insertLast(stuct node **head, int newId) { } 14. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list after a node with the same id as the input parameter...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • C++ Consider the following structure of node and linked list. struct Node { int key; Node...

    C++ Consider the following structure of node and linked list. struct Node { int key; Node *next; }; 10 -> 20 -> 30 -> 10 -> 10 -> 50 -> 10 -> NULL What will be the output of following pseudo-code? Consider head is the pointer to the first node of above linked list. Node *walker = head; int count = 0; while(walker!= NULL && count < 3) { if(walker->key == 10) { count = count + 1; } walker...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • You are given a pointer head to the first node in a linked list. Each node...

    You are given a pointer head to the first node in a linked list. Each node points to the next node. We call the list a snail if the last node points to some node v in the list. We call the list a snake if the last node points to NULL. The list has n nodes. You are not allowed to change the list (permanently or temperately). You are allowed to use O(1) extra memory. The value of n...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

  • Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Nod...

    Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...

  • 2) (10 pts) Write a function that takes in a pointer to a linked list of...

    2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...

  • A linked list is constructed of nodes described by the following structure: struct node{ char data;...

    A linked list is constructed of nodes described by the following structure: struct node{ char data; struct node *next; }; Assume a linked list containing a sentinel node is constructed from the above nodes. Write a function named "count"-prototyped as int count(struct node*sent)- that accepts a pointer to the sentinel node; counts the number of data (non-sentinel) nodes containing the character 'A'; and returns that count as the function value.

  • Define a struct Node to represent a node in a double linked-list that stores a string...

    Define a struct Node to represent a node in a double linked-list that stores a string as data. Write a function to insert a node to the head of the linked list. The function takes two arguments: a pointer to the first node in the double linked list and a string value. It should create a new node with the given value to the head of the double linked list.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT