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