-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathLinkedList
More file actions
118 lines (109 loc) · 2.85 KB
/
Copy pathLinkedList
File metadata and controls
118 lines (109 loc) · 2.85 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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
}*head;
void insertatfirst(int ele){
struct Node *new = (struct Node*)malloc(sizeof(struct Node));
new->data = ele;
if(head==NULL){
new->next = NULL;
}
else{
new->next = head;
}
head = new;
}
void insertatlast(int ele){
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
struct Node *new = (struct Node*)malloc(sizeof(struct Node));
new->data = ele;
temp = head;
if(head==NULL){
new->next = NULL;
head = new;
}
else{
while(temp->next!=NULL){
temp = temp->next;
}
}
temp->next = new;
new->next = NULL;
}
int deletefirst(){
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
if(head==NULL){
printf("Underflow!!");
return 0;
}
else{
temp = head;
head = head->next;
return temp->data;
}
}
int deletelast(){
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
struct Node *prev = (struct Node*)malloc(sizeof(struct Node));
if(head==NULL){
printf("Underflow!!");
return 0;
}
else if(head->next=NULL){
head = NULL;
return 0;
}
else{
temp = head;
while(temp->next!=NULL){
prev = temp;
temp = temp->next;
}
prev->next = NULL;
return temp->data;
}
}
void display(){
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp = head;
printf("\n");
while(temp!=NULL){
printf("%d -> ",temp->data);
temp = temp->next;
}
printf("NULL");
}
void main(){
char yn;
int ch,ele,data;
do{
printf("\n 1: For Insert at Beginning \n 2: For Insert at end\n 3: For Delete at Beginning \n 4: For Delete at end\n 5: For Display\n \n Your choice: ");
scanf("%d",&ch);
switch(ch){
case 1: printf("\nEnter Element you want to Insert: ");
scanf("%d",&ele);
insertatfirst(ele);
break;
case 2: printf("\nEnter Element you want to Insert: ");
scanf("%d",&ele);
insertatlast(ele);
break;
case 3:data = deletefirst();
printf("Deleted data is %d",data);
break;
case 4:data = deletelast();
printf("Deleted data is %d",data);
break;
case 5:display();
break;
default:
printf("Wrong choice!");
}
printf("\n\nDo you want to continue? Enter Y for yes: ");
scanf(" %c",&yn);
}while(yn=='Y'||yn=='y');
getch(); //for holding the screen
}