-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircularll.c
141 lines (127 loc) · 3.04 KB
/
circularll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<stdio.h> //Changes yet to be made for program to work properly
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
void length(struct node *head){
struct node *current = head;
int count = 0;
if(head==NULL)
return;
do{
current = current->next;
count++;
}while(current!=head);
printf("%d",count);
}
void print(struct node *head){
struct node *current = head;
int count = 0;
if(head==NULL)
return;
do{
printf("%d ",current->data);
current = current->next;
}while(current!=head);
}
//Insert new node at the end of Circular Linked List
void insertend(struct node **head, int xe){
struct node *current = *head;
struct node *newnode = (struct node *)malloc(sizeof(struct node));
if(!newnode){
printf("Memory error");
return;
}
newnode->data=xe;
while(current->next!=head)
current=current->next;
newnode->next = newnode;
if(*head==NULL)
*head = newnode;
else{
newnode->next = *head;
current->next = newnode;
}
}
//Insert new node at the front of Circular Linked List
void insertfront(struct node **head, int xf){
struct node *current = *head;
struct node *newnode = (struct node *)malloc(sizeof(struct node));
if(!newnode){
printf("Memory error");
return;
}
newnode->data=xf;
while(current->next!=head)
current=current->next;
newnode->next = newnode;
if(*head==NULL)
*head = newnode;
else{
newnode->next = *head;
current->next = newnode;
*head = newnode;
}
return;
}
//Delete last node from Circular Linked List
void deletelast(struct node **head){
struct node *temp = *head;
struct node *current = *head;
if(*head==NULL){
printf("List empty");
return;
}
while(current->next!=head){
temp = current;
current = current->next;
}
temp->next = current->next;
free(current);
return;
}
//Delete first node from Circular Linked List
void deletefirst(struct node **head){
struct node *temp =*head;
struct node *current = *head;
if(*head==NULL){
printf("List empty");
return;
}
while(current->next!=head)
current = current->next;
current->next = (*head)->next;
*head = (*head)->next;
free(temp);
return;
}
int main(){
int ch,x,y;
struct node *head = NULL;
do{
printf("\nMenu Options:\n1.Insert element at the beginning of Circular Linked List\n2.Insert element at the end of Circular Linked List\n3.Delete element at the front of Circular linked list\n4.Delete element at the end of Circular linked list\n5.Find length of Circular linked list\n6.Print contents of Circular linked list\n7.Exit\nEnter choice: ");
scanf("%d",&ch);
switch(ch){
case 1: printf("Enter element to be inserted at the front: ");
scanf("%d",&x);
insertfront(&head,x);
break;
case 2: printf("Enter element to be inserted at the end: ");
scanf("%d",&y);
insertend(&head,y);
break;
case 3: deletefirst(&head);
break;
case 4: deletelast(&head);
break;
case 5: length(head);
break;
case 6: print(head);
break;
case 7: exit(0);
break;
}
}while(ch!=7);
return 0;
}