|
| 1 | +#include<stdio.h> |
| 2 | +#include<malloc.h> |
| 3 | +struct node |
| 4 | +{ |
| 5 | + int data; |
| 6 | + struct node *next; |
| 7 | +}; |
| 8 | +struct node *start=NULL; |
| 9 | +struct node *create(struct node *); |
| 10 | +struct node *display(struct node *); |
| 11 | +struct node *insert_beg(struct node *); |
| 12 | +struct node *insert_end(struct node *); |
| 13 | +struct node *insert(struct node *); |
| 14 | +struct node *delete_beg(struct node *); |
| 15 | +struct node *delete_end(struct node *); |
| 16 | +struct node *delete_node(struct node *); |
| 17 | + |
| 18 | +int main() |
| 19 | +{ |
| 20 | +int option,a; |
| 21 | +do |
| 22 | +{ |
| 23 | +printf("\n MAIN MENU"); |
| 24 | +printf("\n1.create \n2.display \n 3.insert at begening \n4.insert at the end \n 5.insert at particular position \n6.delete begening"); |
| 25 | +printf("\n 7.delete end \n 8.delete a particular node \n 9.EXIT"); |
| 26 | +printf("\n Enter your option:"); |
| 27 | +scanf("%d",&option); |
| 28 | +switch(option) |
| 29 | +{ |
| 30 | +case 1:start=create(start);printf("\n list created sucessfully");break; |
| 31 | +case 2:start=display(start);break; |
| 32 | +case 3:start=insert_beg(start);break; |
| 33 | +case 4:start=insert_end(start);break; |
| 34 | +case 5:start=insert(start);break; |
| 35 | +case 6:start=delete_end(start);break; |
| 36 | +case 7:start=delete_end(start);break; |
| 37 | +case 8:start=delete_node(start);break; |
| 38 | +case 9:exit(0);break; |
| 39 | +default:printf("\n invalid option enter correct option"); |
| 40 | +} |
| 41 | +printf("\n do you want to continue(1/0):"); |
| 42 | +scanf("%d",&a); |
| 43 | + |
| 44 | +}while(a==1); |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +struct node *create(struct node *start) |
| 49 | +{ |
| 50 | + struct node *ptr,*new_node; |
| 51 | + int num; |
| 52 | + printf("\n press -1 to end"); |
| 53 | + printf("\n enter the data:"); |
| 54 | + scanf("%d",&num); |
| 55 | + while(num!=-1) |
| 56 | + { |
| 57 | + new_node=(struct node *)malloc(sizeof(struct node)); |
| 58 | + new_node->data=num; |
| 59 | + if(start==NULL) |
| 60 | + { |
| 61 | + new_node->next=NULL; |
| 62 | + start=new_node; |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + ptr=start; |
| 67 | + while(ptr->next!=NULL) |
| 68 | + ptr=ptr->next; |
| 69 | + ptr->next=new_node; |
| 70 | + new_node->next=NULL; |
| 71 | + } |
| 72 | + printf("\n enter the data:"); |
| 73 | + scanf("%d",&num); |
| 74 | + } |
| 75 | + return start; |
| 76 | + } |
| 77 | + |
| 78 | +struct node *display(struct node *start) |
| 79 | +{ |
| 80 | + struct node *ptr; |
| 81 | + ptr=start; |
| 82 | + while(ptr!=NULL) |
| 83 | + { |
| 84 | + printf("\t %d",ptr->data); |
| 85 | + ptr=ptr->next; |
| 86 | + } |
| 87 | + return start; |
| 88 | + } |
| 89 | + |
| 90 | +struct node *insert_beg(struct node *start) |
| 91 | +{ |
| 92 | + struct node *new_node; |
| 93 | + int num; |
| 94 | + printf("\n enter the value of data:"); |
| 95 | + scanf("%d",&num); |
| 96 | + new_node=(struct node *)malloc(sizeof(struct node)); |
| 97 | + new_node->data=num; |
| 98 | + new_node->next=start; |
| 99 | + start=new_node; |
| 100 | + return start; |
| 101 | + |
| 102 | +} |
| 103 | +struct node *insert_end(struct node *start) |
| 104 | +{ |
| 105 | + struct node *ptr,*new_node; |
| 106 | + int num; |
| 107 | + printf("\n enter the value of data:"); |
| 108 | + scanf("%d",&num); |
| 109 | + new_node=(struct node *)malloc(sizeof(struct node)); |
| 110 | + new_node->data=num; |
| 111 | + new_node->next=NULL; |
| 112 | + ptr=start; |
| 113 | + while(ptr->next!=NULL) |
| 114 | + ptr=ptr->next; |
| 115 | + ptr->next=new_node; |
| 116 | + return start; |
| 117 | +} |
| 118 | + |
| 119 | +struct node *insert(struct node *start) |
| 120 | +{ |
| 121 | + struct node *ptr,*new_node,*preptr; |
| 122 | + int num,val; |
| 123 | + printf("\n enter the value of data:"); |
| 124 | + scanf("%d",&num); |
| 125 | + printf("\n enter the value before the data has to be inserted:"); |
| 126 | + scanf("%d",&val); |
| 127 | + new_node=(struct node *)malloc(sizeof(struct node)); |
| 128 | + new_node->data=num; |
| 129 | + ptr=start; |
| 130 | + while(ptr->data!=val) |
| 131 | + { |
| 132 | + preptr=ptr; |
| 133 | + ptr=ptr->next; |
| 134 | + if(ptr==NULL) |
| 135 | + printf("\n there is no element specified...."); |
| 136 | + } |
| 137 | + if(ptr==NULL) |
| 138 | + printf("\n there is no element specified...."); |
| 139 | + preptr->next=new_node; |
| 140 | + new_node->next=ptr; |
| 141 | + return start; |
| 142 | + } |
| 143 | + |
| 144 | +struct node *delete_beg(struct node *start) |
| 145 | +{ |
| 146 | + struct node *ptr; |
| 147 | + ptr=start; |
| 148 | + start=start->next; |
| 149 | + free(ptr); |
| 150 | + return start; |
| 151 | +} |
| 152 | + |
| 153 | +struct node *delete_end(struct node *start) |
| 154 | +{ |
| 155 | + struct node * ptr,*preptr; |
| 156 | + ptr=start; |
| 157 | + while(ptr->next!=NULL) |
| 158 | + { |
| 159 | + preptr=ptr; |
| 160 | + ptr=ptr->next; |
| 161 | + } |
| 162 | + preptr->next=NULL; |
| 163 | + free(ptr); |
| 164 | + return start; |
| 165 | +} |
| 166 | +struct node *delete_node(struct node *start) |
| 167 | +{ |
| 168 | + struct node * ptr,*preptr; |
| 169 | + int val; |
| 170 | + printf("\n enter the value of node to be deleted:"); |
| 171 | + scanf("%d",&val); |
| 172 | + ptr=start; |
| 173 | + if(ptr->data==val) |
| 174 | + { |
| 175 | + start=delete_beg(start); |
| 176 | + return start; |
| 177 | + } |
| 178 | + else |
| 179 | + { |
| 180 | + while(ptr->data!=val) |
| 181 | + { |
| 182 | + preptr=ptr; |
| 183 | + ptr=ptr->next; |
| 184 | + if(ptr==NULL) |
| 185 | + printf("\n there is no element specified...."); |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + preptr->next=ptr->next; |
| 190 | + free(ptr); |
| 191 | + return start; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
0 commit comments