-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlinkedList.c
137 lines (120 loc) · 2.86 KB
/
linkedList.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
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
void insertNode(struct Node **head_ref){
struct Node *temp = malloc(sizeof(struct Node));
int val;
printf("\n Enter the data for the node: ");
scanf("%d", &val);
temp->data = val;
temp->next = *head_ref;
*head_ref = temp;
}
void printList(struct Node *n){
while(n!= NULL){
if(n->next == NULL)
printf("%d",n->data);
else
printf("%d -> ",n->data);
n=n->next;
}
}
void deleteNode(struct Node **head_ref, int key){
struct Node *temp = *head_ref, *prev;
if(temp!=NULL && temp->data == key){
*head_ref = temp->next;
free(temp);
return;
}
while(temp!= NULL && temp->data != key){
prev= temp;
temp=temp->next;
}
if (temp==NULL) return;
prev->next =temp->next;
free(temp);
}
void printMiddle(struct Node *head_ref){
struct Node *doubled = head_ref;
struct Node *single = head_ref;
while(doubled->next != NULL){
doubled= doubled->next->next;
single=single->next;
}
printf("\n%d is the Middle Element.",single->data);
}
void reverse(struct Node **head)
{
if (!head)
return;
reverseUtil(*head, NULL, head);
}
// A simple and tail recursive function to reverse
// a linked list. prev is passed as NULL initially.
void reverseUtil(struct Node *curr,struct Node *prev,struct Node **head)
{
/* If last node mark it head*/
if (!curr->next)
{
*head = curr;
/* Update next to prev node */
curr->next = prev;
return;
}
/* Save curr->next node for recursive call */
struct Node *next = curr->next;
/* and update next ..*/
curr->next = prev;
reverseUtil(next, curr, head);
}
void printReverse(struct Node *head_ref)
{
struct Node *next = malloc(sizeof(struct Node));
if(head_ref->next != NULL)
{
next = head_ref->next;
printReverse(next);
}
printf("%d ", head_ref->data);
}
int main(){
struct Node* head = malloc(sizeof(struct Node));
struct Node *current = head;
struct Node *prev = NULL;
struct Node *next = NULL;
int ch=0, inp =0;
while(ch!=-1){
printf("\n\t\tLinked List\n1. Create a node.\n2. Delete a node with specific value.\n3. Print Middle Element.\n4. Reverse Linked List.\n5. Print Reverse Linked List.\n Enter -1 to exit.\nEnter your choice: ");
scanf("%d",&ch);
if(ch == -1) return 0;
switch(ch){
case 1:
insertNode(&head);
printList(head);
break;
case 2:
printf("Enter the value to be deleted: ");
scanf("%d", &inp);
deleteNode(&head, inp);
printList(head);
break;
case 3:
printMiddle(head);
break;
case 4:
reverse(&head);
printList(head);
break;
case 5:
printReverse(head);
break;
default:
printf("\nWrong coice. Try again.");
break;
}
}
return 0;
}