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...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
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...
Show how circular linked list can be useful to implement Circular Queue (CQ) ADT (Abstract Data...
Show how circular linked list can be useful to implement Circular Queue (CQ) ADT (Abstract Data Type). Compare and contrast with array based implementation. Which one would you recommend to implement CQ and why
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++ What Should This Program Do? Linked List Class Design your own linked list class (List.h)...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h) to hold a series of strings. The linked list node should be implemented as a struct. The class should have member functions for appending, inserting, and deleting nodes. You should also have a display function that will traverse the list & display each node’s value. Don’t forget to add a destructor that destroys the list. DRIVER – driver.cpp Write a driver program (driver.cpp) that...
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)
We are working with a circular linked list that is referenced by a tail reference, i.e.,...
We are working with a circular linked list that is referenced by a tail reference, i.e., a reference to the last node of the linked list.  There is no head or size information about the linked list. Node is declared as: Node {       int value;       Node next; } There are four parts in this problem. Don't forget to deal with special cases for each part. Write instructions to insert a node newNode at the beginning of the circular linked list. Write...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT