1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdbool.h>
5
6struct node {
7 int data;
8 int key;
9
10 struct node *next;
11};
12
13struct node *head = NULL;
14struct node *current = NULL;
15
16bool isEmpty() {
17 return head == NULL;
18}
19
20int length() {
21 int length = 0;
22
23 //if list is empty
24 if(head == NULL) {
25 return 0;
26 }
27
28 current = head->next;
29
30 while(current != head) {
31 length++;
32 current = current->next;
33 }
34
35 return length;
36}
37
38//insert link at the first location
39void insertFirst(int key, int data) {
40
41 //create a link
42 struct node *link = (struct node*) malloc(sizeof(struct node));
43 link->key = key;
44 link->data = data;
45
46 if (isEmpty()) {
47 head = link;
48 head->next = head;
49 } else {
50 //point it to old first node
51 link->next = head;
52
53 //point first to new first node
54 head = link;
55 }
56}
57
58//delete first item
59struct node * deleteFirst() {
60
61 //save reference to first link
62 struct node *tempLink = head;
63
64 if(head->next == head) {
65 head = NULL;
66 return tempLink;
67 }
68
69 //mark next to first link as first
70 head = head->next;
71
72 //return the deleted link
73 return tempLink;
74}
75
76//display the list
77void printList() {
78
79 struct node *ptr = head;
80 printf("\n[ ");
81
82 //start from the beginning
83 if(head != NULL) {
84
85 while(ptr->next != ptr) {
86 printf("(%d,%d) ",ptr->key,ptr->data);
87 ptr = ptr->next;
88 }
89 }
90
91 printf(" ]");
92}
93
94void main() {
95 insertFirst(1,10);
96 insertFirst(2,20);
97 insertFirst(3,30);
98 insertFirst(4,1);
99 insertFirst(5,40);
100 insertFirst(6,56);
101
102 printf("Original List: ");
103
104 //print list
105 printList();
106
107 while(!isEmpty()) {
108 struct node *temp = deleteFirst();
109 printf("\nDeleted value:");
110 printf("(%d,%d) ",temp->key,temp->data);
111 }
112
113 printf("\nList after deleting all items: ");
114 printList();
115}
1#include <iostream>
2using namespace std;
3
4#define NULL 0
5
6
7struct node
8{
9 int data ;
10 struct node *next ;
11} ;
12
13struct node *first=NULL ;
14struct node *last=NULL ;
15
16void create()
17{
18 int i , n ;
19 struct node *pnode , *p ;
20
21 printf("Enter the number of nodes required:\n") ;
22 scanf("%d",&n) ;
23
24 printf("Enter the data value of each node:\n") ;
25 for(i=1 ; i<=n ; i++)
26 {
27 pnode=(struct node*)malloc(sizeof(struct node)) ;
28 if(pnode==NULL)
29 {
30 printf("Memory overflow. Unable to create.\n") ;
31 return ;
32 }
33
34 scanf("%d",&pnode->data) ;
35
36 if(first==NULL)
37 first=last=pnode ;
38 else
39 {
40 last->next=pnode ;
41 last=pnode ; /* last keeps track of last node */
42 }
43
44 last->next=first ;
45 }
46}
47
48/* This function will delete a node with value k from the Linked List if such a node exists */
49void deletenode(int k)
50{
51 struct node *p , *follow ;
52
53 /* searching the required node */
54 p=first ;
55 follow=NULL ;
56 while(follow!=last)
57 {
58 if(p->data==k)
59 break ;
60 follow=p ;
61 p=p->next ;
62 }
63
64 if(follow==last)
65 printf("Required node not found.\n") ;
66 else
67 {
68 if(p==first&&p==last) /* deleting the one and the only node */
69 first=last=NULL ;
70 else if(p==first) /* deleting the first node */
71 {
72 first=first->next ;
73 last->next=first ;
74 }
75 else if(p==last) /* deleting the last node */
76 {
77 last=follow ;
78 last->next=first ;
79 }
80 else /* deleting any other node */
81 follow->next=p->next ;
82
83 free(p) ;
84 }
85}
86
87/* This function will go through all the nodes of Linked List exactly once and will display data value of each node */
88void traverse()
89{
90 struct node *p , *follow ;
91 if(first==NULL)
92 printf("Circularly Linked List Empty") ;
93 else
94 {
95 printf("Circularly Linked List is as shown: \n") ;
96
97 p=first ;
98 follow = NULL ;
99 while(follow!=last)
100 {
101 printf("%d " , p->data) ;
102 follow=p ;
103 p=p->next ;
104 }
105
106 printf("\n") ;
107 }
108}
109
110int main()
111{
112 int x , k , ch ;
113
114 do
115 {
116 printf("\n Menu: \n") ;
117 printf("1:Create Linked List \n") ;
118 printf("2:Delete Node \n") ;
119 printf("3:Traverse \n") ;
120 printf("4:Exit \n") ;
121
122 printf("\nEnter your choice: ") ;
123 scanf("%d",&ch) ;
124
125 switch(ch)
126 {
127 case 1:
128 create() ;
129 break ;
130
131 case 2:
132 printf("Enter the data value of the node to be deleted: ") ;
133 scanf("%d",&k) ;
134 deletenode(k) ;
135 break ;
136
137 case 3:
138 traverse() ;
139 break ;
140
141 case 4:
142 break ;
143 }
144 }
145 while(ch!=4) ;
146
147 return 0;
148}
149
1#include<iostream>
2
3#define SIZE 100
4
5using namespace std;
6
7class node
8{
9public:
10 node()
11 {
12 next = NULL;
13 }
14 int data;
15 node *next;
16}*front=NULL,*rear=NULL,*n,*temp,*temp1;
17
18class cqueue
19{
20public:
21 void insertion();
22 void deletion();
23 void display();
24};
25
26int main()
27{
28 cqueue cqobj;
29 int ch;
30 do
31 {
32 cout<<"\n\n\tMain Menu";
33 cout<<"\n##########################";
34 cout<<"\n1. Insert\n2. Delete\n3. Display\n4. Exit\n\nEnter Your Choice: ";
35 cin>>ch;
36 switch(ch)
37 {
38 case 1:
39 cqobj.insertion();
40 cqobj.display();
41 break;
42 case 2:
43 cqobj.deletion();
44 break;
45 case 3:
46 cqobj.display();
47 break;
48 case 4:
49 break;
50 default:
51 cout<<"\n\nWrong Choice!!! Try Again.";
52 }
53 }while(ch!=4);
54 return 0;
55}
56
57void cqueue::insertion()
58{
59 n=new node[sizeof(node)];
60 cout<<"\nEnter the Element: ";
61 cin>>n->data;
62 if(front==NULL)
63 {
64 front=n;
65 }
66 else
67 {
68 rear->next=n;
69 }
70 rear=n;
71 rear->next=front;
72}
73
74void cqueue::deletion()
75{
76 int x;
77 temp=front;
78 if(front==NULL)
79 {
80 cout<<"\nCircular Queue Empty!!!";
81 }
82 else
83 {
84 if(front==rear)
85 {
86 x=front->data;
87 delete(temp);
88 front=NULL;
89 rear=NULL;
90 }
91 else
92 {
93 x=temp->data;
94 front=front->next;
95 rear->next=front;
96 delete(temp);
97 }
98 cout<<"\nElement "<<x<<" is Deleted";
99 display();
100 }
101}
102
103void cqueue::display()
104{
105 temp=front;
106 temp1=NULL;
107 if(front==NULL)
108 {
109 cout<<"\n\nCircular Queue Empty!!!";
110 }
111 else
112 {
113 cout<<"\n\nCircular Queue Elements are:\n\n";
114 while(temp!=temp1)
115 {
116 cout<<temp->data<<" ";
117 temp=temp->next;
118 temp1=front;
119 }
120 }
121}
1/* Function to traverse a given Circular linked list and print nodes */
2void printList(struct Node *first)
3{
4 struct Node *temp = first;
5
6 // If linked list is not empty
7 if (first != NULL)
8 {
9 // Keep printing nodes till we reach the first node again
10 do
11 {
12 printf("%d ", temp->data);
13 temp = temp->next;
14 }
15 while (temp != first);
16 }
17}