Question

In: Computer Science

how to do circular linked list in c++ (inset and delet and display)

how to do circular linked list in c++
(inset and delet and display)

Solutions

Expert Solution

Please find the answer below, all the details are mentioned in the comments.

circular.cpp

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

//structure of the node
struct node {
int data;//to store data
node *next;//to store next
};

//function to delete node form list from the begin
void deleteNode(struct node *&head){
//if list is empty
if(head==NULL){
cout<<"No elements to delete.\n";
}
//if head is the only element in the list
else if(head->next==head){
cout<<head->data<<" deleted.\n";
delete head;//delete head
head = NULL; //set head to NULL
}
//if there is more then one node in the list
else{
//store the head in temp
node *temp;
temp = head;
//last node of the list
node *last;
last = head;
while(last->next!=head)
last = last->next;
//update last to point to head next
last->next = head->next;
cout<<head->data<<" deleted.\n";
//head would be head->next node
head = last->next;
//delete temp node
delete temp;
}
}

//insertNode at the end.
void insertNode (int data, struct node *&head){
node *newNode;
newNode= new node;//creates new node.
newNode->data=data;//stores value.

//If the list is empty, create a single node
if(head==NULL){
newNode->next = newNode;
head = newNode;
}
//if head is the only element in the list
else if(head == head->next){
newNode->next = head->next;
head->next = newNode;
}
// If list is not empty
else{
node *temp;
//last node of the list
temp = head;
while(temp->next!=head)
temp = temp->next;
//next of new node is head
newNode->next = temp->next;
//next of last node is newNode
temp->next = newNode;
}
}
//display th list in doubly link manner
void display(struct node* head) {
node *temp;
temp = head;

printf("\nThe List is:\n");
if(head == NULL){
cout<<"Empty"<<endl;
return;
}
//traverse the list from begin
while (temp->next != head)
{
cout<<temp->data<<"-->";
temp = temp->next;
}
cout<<temp->data<<endl;
}

int main()
{
//head of the list
struct node *head=NULL;
insertNode(1,head);//insert node into the list
insertNode(2,head);//insert node into the list
insertNode(3,head);//insert node into the list
insertNode(4,head);//insert node into the list
insertNode(5,head);//insert node into the list
display(head);
deleteNode(head);
deleteNode(head);
deleteNode(head);
display(head);
deleteNode(head);
deleteNode(head);
display(head);
return 0;
}

Output:

Please let us know in the comments if you face any problems.


Related Solutions

A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use the preprocessor directives <iostream> and using namespace std; You must use this prototype: int remove_every_other(node *&rear)
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT