-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab9.c
163 lines (160 loc) · 4.71 KB
/
lab9.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* A program to implement circular linked list and develop functions to perform
operations like insertion, deletion and linear search */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
struct Node
{
int data;
struct Node *next; //next is pointer of the last node
} *head = NULL; //head points to the first node
//creating a function to insert a new node in the end of list
void insertion(int element)
{
struct Node *node = head;
// IF head node is not present
if (node == NULL)
{
node = (struct Node *)malloc(sizeof(struct Node)); //Dynamically creating the memory for new node
node->data = element;
node->next = node; //Change next of new node to point to head
head = node; //Change head to point to recently created node
}
else
{
/* if the list is not empty,
shift to the last node pointing towards NULL and dynamically create a new node*/
while (node->next != head)
{
node = node->next;
}
// Now node is pointing to last node
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
node->next = newNode; //pointing the next of last node to the newNode created
newNode->data = element; //inserting the data value in the newNode
newNode->next = head; //pointing the next of newNode back to the head
}
}
//creating a function to delete a node from the beginning
int deletion(int element)
{
int removed;
if (head == NULL)
printf("\nList is empty\n");
else {
struct Node *temp = head;
removed = head->data;
/*shifting to the last node and then to the temp node created until next of temp doesn't point towards head */
do
{
temp = temp->next;
}while (temp->next != head);
temp->next = head->next; //pointing the next of temp to the next of head node
temp = head; //setting temp to head
head = head->next; //now pointing the head to the next of previous head node
free(temp); //freeing the memory of temp
}
return removed;
}
//creating a function to search element in the list traversely
int linearSearch(int data)
{
struct Node *node = head;
int index = -1;
int count = 0;
if(head != NULL)
{
do
{
if (node->data == data)
{
index = count;
break;
}
count++;
node = node->next;
}while(node != head);
}
return index;
}
//creating a function to display all the nodes data from the list circularly
void print()
{
struct Node *node = head;
if (node == NULL)
{
printf("\nList is Empty.\n");
}
else
{
printf("\nPrinting the list:\n");
//it will traverse upto the last node and print the data values from head node to last node
do
{
printf("%d->", node->data);
node = node->next;
}while(node != head);
//to check its circulairty printing again the data value of head node
printf("%d->", node->data);
printf("\n");
}
}
int main()
{
int element, operation;
printf("\n\n\t\t\t\t\t\tWelcome Back Arjun\t\t\n");
do
{
printf("\nEnter the operation\n");
printf("1.Insertion\n");
printf("2.Deletion\n");
printf("3.Linear Search\n");
printf("4.Display\n");
printf("5.Exit\n");
scanf("%d", &operation);
switch (operation)
{
case 1:
printf("\nEnter the element you want to insert:\n");
scanf("%d", &element);
insertion(element);
break;
case 2:;
int removeElement;
int remove = deletion (removeElement);
if (remove != INT_MIN)
{
printf("%d Removed.", remove);
}
else
{
printf("No element Like that found");
}
break;
case 3:;
int searchElement;
printf("\nEnter the element you want to search\n");
scanf("%d", &searchElement);
int search = linearSearch(searchElement);
if (search != -1)
{
printf("\nElement found at index : %d\n", search);
}
else{
printf("\nElement not found\n");
}
break;
case 4:
print();
break;
case 5:
exit(0);
break;
default:
printf("Wrong Choice\n");
break;
}
} while (1);
return 0;
}