-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist4.c
97 lines (85 loc) · 2.03 KB
/
linkedlist4.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
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = 0;
struct node *temp , *newnode;
int insertattheend();
int insertatthebeginning();
int delatthebeginning();
int deleteattheend();
int display();
int main() {
int ch, pos;
while(1){
printf("-----------Operations of Linked List-----------\n");
printf("1. Insert at the beginning\n");
printf("2. Insert at end\n");
printf("3. Insert at a Position\n");
printf("4. Delete at the beginning\n");
printf("5. Delete at the end\n");
printf("6. Display\n");
printf("7. Exit\n");
printf("Enter your Choice:\n");
scanf("%d" , &ch);
switch(ch){
case 1:insertatthebeginning();
break;
case 2: insertattheend();
case 4: delatthebeginning();
break;
case 5: deleteattheend();
case 6: display();
break;
case 7: exit(0);
default: printf("Invalid Choice!");
}
}
return 0;
}
int insertatthebeginning(){
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: \n");
scanf("%d" , &newnode -> data);
newnode -> next = head;
head = newnode;
int insertattheend(){
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: \n");
scanf("%d" , &newnode -> data);
newnode -> next = 0;
if(head == 0){
head = temp = newnode;
} else {
temp -> next = newnode;
temp = newnode;
}
}
int delatthebeginning(){
struct node *a , *prevnode;
a = head;
head = head -> next
free(a);
}
int delattheend(){
struct node *a , *prevnode;
a = head;
while(a-> next != 0){
prevnode = a;
a = a -> next;
}
prevnode -> next = 0;
free(a);
}
int display(){
struct node *k;
k = head;
while(k != NULL)
{
printf("%d\t" , k -> data);
k = k -> next;
}
printf("\n");
}