-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathLinkedList.c
More file actions
160 lines (156 loc) · 3.5 KB
/
Copy pathLinkedList.c
File metadata and controls
160 lines (156 loc) · 3.5 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
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode
{
int data;
struct ListNode *next;
}node;
node *createList(node *head, int num);
node *insertNodeAtTail(node *head, int data);
node *insertNodeAtHead(node *head, int data);
void DeleteNode(node *head, int pos);
void traverse(node *head);
node *createList(node *head, int num)
{
node *temp, *newNode;
int data;
if(head != NULL)
{
printf("Node already exists");
return head;
}
for(int i=0; i<num; i++)
{
printf("Enter data: ");
scanf("%d",&data);
newNode = (node*)malloc(sizeof(node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL)
{
head = newNode;
temp = newNode;
}
else
{
temp->next = newNode;
temp = newNode;
}
}
return head;
}
node *insertNodeAtTail (node *head, int data)
{
node* newNode, *temp;
newNode = (node*)malloc(sizeof(node));
newNode->data = data;
if(head == NULL)
{
head = newNode;
newNode->next = NULL;
return head;
}
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
newNode->next = NULL;
return head;
}
node *insertNodeAtHead(node *head, int data)
{
node *newNode = malloc(sizeof(newNode));
newNode->data = data;
if(head == NULL)
{
head = newNode;
return head;
}
newNode->next = head;
head = newNode;
return head;
}
void traverse(node *head)
{
node *temp;
temp = head;
while (temp != NULL)
{
printf(" --> %d",temp->data);
temp = temp->next;
}
printf("\n");
}
void DeleteNode(node *head, int pos)
{
node *temp, *temp1;
int cnt = 1;
if(head == NULL)
{
return;
}
if(pos == 1)
{
temp = head;
head = head->next;
free(temp);
}
temp = head;
while(cnt < pos-1 && temp != NULL)
{
temp = temp->next;
cnt++;
}
if(temp != NULL)
{
temp1 = temp->next;
temp->next = temp1->next;
free(temp1);
}
}
int main()
{
node *head = NULL;
int choice, size, data, pos;
while(1)
{
printf("\n1. Create a List \n2. Insert a node after the linked list");
printf("\n3. Insert a node before the linked lis \n4. Traverse and print the content of the list");
printf("\n5. Delete Node\n6. Exit \nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter size of the list: ");
scanf("%d",&size);
head = createList(head, size);
break;
case 2:
printf("Enter the data of the node: ");
scanf("%d",&data);
head = insertNodeAtTail(head, data);
break;
case 3:
printf("Enter the data of the node: ");
scanf("%d",&data);
head = insertNodeAtHead(head, data);
break;
case 4:
traverse(head);
break;
case 5:
printf("Enter the position no: ");
scanf("%d", &pos);
DeleteNode(head, pos);
break;
case 6:
printf("End");
exit(0);
default:
printf("Invalid Input");
}
}
return 0;
}