-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List_Operations.c
More file actions
161 lines (152 loc) · 3.79 KB
/
Linked_List_Operations.c
File metadata and controls
161 lines (152 loc) · 3.79 KB
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
#include<stdio.h>
#include<stdlib.h>
struct Node* insert_at_head(struct Node*);
struct Node* insert_at_tail(struct Node*);
struct Node* delete(struct Node*);
void print(struct Node*);
struct Node* reverse_iterative(struct Node*);
struct Node* reverse_recursion(struct Node*);
struct Node
{
int data;
struct Node* next;
};
//Insert at head of a singly linked list
struct Node* insert_at_head(struct Node* head)
{
int n, x;
printf("How many numbers?\n");
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
printf("Enter the number:\n");
scanf("%d",&x);
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp -> data = x;
temp -> next = head;
head = temp;
}
return head;
}
//Insert at tail of a singly linked list
struct Node* insert_at_tail(struct Node* head)
{
int n, x;
printf("How many numbers?\n");
scanf("%d",&n);
struct Node* tail = head;
for(int i = 0; i < n; i++)
{
printf("Enter the number:\n");
scanf("%d",&x);
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp -> data = x;
temp -> next = NULL;
if(i == 0) head = temp;
if(i != 0) tail -> next = temp;
tail = temp;
}
return head;
}
struct Node* delete(struct Node* head)
{
int pos;
printf("Enter the position of element to delete:\n");
scanf("%d",&pos);
struct Node* temp = head;
if(pos == 1)
{
head = temp -> next;
free(temp);
return(head);
}
for(int i = 1; i < pos - 1; i++)
temp = temp -> next;
struct Node* temp1 = temp -> next;
temp -> next = temp1 -> next;
free(temp1);
return head;
}
struct Node* reverse_iterative(struct Node* head)
{
struct Node* prev = NULL, *current, *after;
current = head;
while(current != NULL)
{
after = current -> next;
current -> next = prev;
prev = current;
current = after;
}
head = prev;
return head;
}
struct Node* reverse_recursion(struct Node* head)
{
if(head -> next == NULL) return head;
struct Node* p = reverse_recursion(head -> next);
head -> next -> next = head;
head -> next = NULL;
return p;
}
//Print elements of Linked List
void print(struct Node* head)
{
struct Node* temp = head;
printf("\nThe Linked List is:\n");
while(temp != NULL)
{
printf("%d\t", temp -> data);
temp = temp -> next;
}
printf("\n");
}
void main()
{
struct Node* head = NULL;
int option = 0;
while(option != 7)
{
printf("\nLinked List operations:\n");
printf("1. Insert at head of Singly Linked List\n");
printf("2. Insert at tail of Singly Linked List\n");
printf("3. Delete from Singly Linked List\n");
printf("4. Reverse the Singly Linked List using iteration\n");
printf("5. Reverse the Singly Linked List using recursion\n");
printf("6. Print the Linked List\n");
printf("7. Exit\n");
printf("\nEnter your option:\n");
scanf("%d",&option);
switch(option)
{
case 1:
head = insert_at_head(head);
print(head);
break;
case 2:
head = insert_at_tail(head);
print(head);
break;
case 3:
head = delete(head);
print(head);
break;
case 4:
head = reverse_iterative(head);
print(head);
break;
case 5:
head = reverse_recursion(head);
print(head);
break;
case 6:
print(head);
break;
case 7:
printf("\nExiting!!!\n");
break;
default:
printf("\nInvalid option\n");
}
}
}