|
| 1 | +#include<stdlib.h> |
| 2 | +#include<malloc.h> |
| 3 | +void create(); |
| 4 | +void display(); |
| 5 | +void insert_begin(); |
| 6 | +void insert_end(); |
| 7 | +void insert_pos(); |
| 8 | +void delete_begin(); |
| 9 | +void delete_end(); |
| 10 | +void delete_pos(); |
| 11 | + |
| 12 | +struct node //Representation of a node |
| 13 | +{ |
| 14 | + int num; |
| 15 | + struct node *link; // struct node enna datatypeilekk point cheyyunna pointeran link |
| 16 | +}; |
| 17 | +struct node *start=NULL; |
| 18 | +int main() |
| 19 | +{ |
| 20 | + int choice; |
| 21 | + while(1){ // while 1 means while true |
| 22 | + printf("\n LINKED LIST OPERATIONS \n"); |
| 23 | + printf("\n MENU \n"); |
| 24 | + printf("\n 1.Create \n"); |
| 25 | + printf("\n 2.Display \n"); |
| 26 | + printf("\n 3.Insert at the beginning \n"); |
| 27 | + printf("\n 4.Insert at the end \n"); |
| 28 | + printf("\n 5.Insert at specified position \n"); |
| 29 | + printf("\n 6.Delete from beginning \n"); |
| 30 | + printf("\n 7.Delete from the end \n"); |
| 31 | + printf("\n 8.Delete from specified position \n"); |
| 32 | + printf("\n 9.Exit \n"); |
| 33 | + printf("\n ------ \n"); |
| 34 | + printf("Enter your choice:\t"); |
| 35 | + scanf("%d",&choice); |
| 36 | + switch(choice) |
| 37 | + { |
| 38 | + case 1: |
| 39 | + create(); |
| 40 | + break; |
| 41 | + case 2: |
| 42 | + display(); |
| 43 | + break; |
| 44 | + case 3: |
| 45 | + insert_begin(); |
| 46 | + break; |
| 47 | + case 4: |
| 48 | + insert_end(); |
| 49 | + break; |
| 50 | + case 5: |
| 51 | + insert_pos(); |
| 52 | + break; |
| 53 | + case 6: |
| 54 | + delete_begin(); |
| 55 | + break; |
| 56 | + case 7: |
| 57 | + delete_end(); |
| 58 | + break; |
| 59 | + case 8: |
| 60 | + delete_pos(); |
| 61 | + break; |
| 62 | + case 9: |
| 63 | + exit(0); |
| 64 | + break; |
| 65 | + default: |
| 66 | + printf("\n Wrong Choice:\n"); |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + return 0; //since the function main has a return type int |
| 71 | +} |
| 72 | +void create() // Function to create a new node |
| 73 | +{ |
| 74 | + struct node *temp,*ptr; |
| 75 | + temp=(struct node *)malloc(sizeof(struct node)); // statement for dynamic memory allocation of a node |
| 76 | + if(temp==NULL) |
| 77 | + { |
| 78 | + printf("\nOut of Memory Space:\n"); |
| 79 | + exit(0); |
| 80 | + } |
| 81 | + printf("\nEnter the data value for the node:\t"); // For reading the value of a node |
| 82 | + scanf("%d",&temp->num); |
| 83 | + temp->link=NULL; // The link to the link node is as null |
| 84 | + if(start==NULL) //Checking if it is first node |
| 85 | + { |
| 86 | + start=temp; //To set the starting address in start(basically linking the firt node to start address. |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + ptr=start; |
| 91 | + while(ptr->link!=NULL) //link is the link field of the node |
| 92 | + { |
| 93 | + ptr=ptr->link; // For traversing b/w nodes |
| 94 | + } |
| 95 | + ptr->link=temp; //Linking existing node's address to the new node |
| 96 | + } |
| 97 | +} |
| 98 | +void display() |
| 99 | +{ |
| 100 | + struct node *ptr; //Declaring a node pointng to the structure variable |
| 101 | + if(start==NULL) |
| 102 | + { |
| 103 | + printf("\nList is empty:\n"); |
| 104 | + return; |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + ptr=start; //start contains the address of the first node |
| 109 | + printf("\nThe List elements are:\n"); |
| 110 | + while(ptr!=NULL) |
| 111 | + { |
| 112 | + printf("%d\t",ptr->num ); |
| 113 | + ptr=ptr->link ; //traversing |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +void insert_begin() //inserting a node at the beginning of the linked list |
| 118 | +{ |
| 119 | + struct node *temp; |
| 120 | + temp=(struct node *)malloc(sizeof(struct node)); |
| 121 | + if(temp==NULL) |
| 122 | + { |
| 123 | + printf("\nOut of Memory Space:\n"); |
| 124 | + return; |
| 125 | + } |
| 126 | + printf("\nEnter the data value for the node:\t" ); |
| 127 | + scanf("%d",&temp->num); |
| 128 | + temp->link =NULL; //Setting the link of temp to null |
| 129 | + if(start==NULL) |
| 130 | + { |
| 131 | + start=temp; |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + temp->link=start; //Linking the new node to start;where start is the address of the first node |
| 136 | + start=temp;// removing start's link to the firt node |
| 137 | + } |
| 138 | +} |
| 139 | +void insert_end() //inserting a node at the beginning of the list |
| 140 | +{ |
| 141 | + struct node *temp,*ptr; |
| 142 | + temp=(struct node *)malloc(sizeof(struct node)); |
| 143 | + if(temp==NULL) |
| 144 | + { |
| 145 | + printf("\nOut of Memory Space:\n"); |
| 146 | + return; |
| 147 | + } |
| 148 | + printf("\nEnter the data value for the node:\t" ); |
| 149 | + scanf("%d",&temp->num ); |
| 150 | + temp->link =NULL; |
| 151 | + if(start==NULL) |
| 152 | + { |
| 153 | + start=temp; |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + ptr=start; |
| 158 | + while(ptr->link !=NULL) |
| 159 | + { |
| 160 | + ptr=ptr->link ; |
| 161 | + } |
| 162 | + ptr->link =temp; //THe link of the last node is set to temp |
| 163 | + } |
| 164 | +} |
| 165 | +void insert_pos() //inserting a node at a specific function. |
| 166 | +{ |
| 167 | + struct node *ptr,*temp; |
| 168 | + int i,pos; |
| 169 | + temp=(struct node *)malloc(sizeof(struct node)); |
| 170 | + if(temp==NULL) |
| 171 | + { |
| 172 | + printf("\nOut of Memory Space:\n"); |
| 173 | + return; |
| 174 | + } |
| 175 | + printf("\nEnter the position for the new node to be inserted:\t"); |
| 176 | + scanf("%d",&pos); |
| 177 | + printf("\nEnter the data value of the node:\t"); |
| 178 | + scanf("%d",&temp->num); |
| 179 | + temp->link=NULL; |
| 180 | + if(pos==0) //if the user set the position to beginning |
| 181 | + { |
| 182 | + temp->link=start; //setting the link field of temp to start |
| 183 | + start=temp; |
| 184 | + } |
| 185 | + else |
| 186 | + { |
| 187 | + for(i=0,ptr=start;i<pos-1;i++) |
| 188 | + { |
| 189 | + ptr=ptr->link; //travering the linked list |
| 190 | + if(ptr==NULL) |
| 191 | + { |
| 192 | + printf("\nPosition not found\n"); |
| 193 | + return; |
| 194 | + } |
| 195 | + } |
| 196 | + temp->link =ptr->link; //Here we are setting the link field of the new node(temp) to position where the link field of node at the poition points too. |
| 197 | + ptr->link=temp; |
| 198 | + } |
| 199 | +} |
| 200 | +void delete_begin() //To delete from the beginning |
| 201 | +{ |
| 202 | + struct node *ptr; |
| 203 | + if(ptr==NULL) |
| 204 | + { |
| 205 | + printf("\nList is Empty:\n"); |
| 206 | + return; |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + ptr=start; |
| 211 | + start=start->link; |
| 212 | + printf("\nThe deleted element is :%d\t",ptr->num); |
| 213 | + free(ptr); //for removing the deleted node from memory |
| 214 | + } |
| 215 | +} |
| 216 | +void delete_end() //To delete from the end |
| 217 | +{ |
| 218 | + struct node *temp,*ptr; |
| 219 | + if(start==NULL) |
| 220 | + { |
| 221 | + printf("\nList is Empty:"); |
| 222 | + exit(0); |
| 223 | + } |
| 224 | + else if(start->link==NULL) |
| 225 | + { |
| 226 | + ptr=start; |
| 227 | + start=NULL; |
| 228 | + printf("\nThe deleted element is:%d\t",ptr->num); |
| 229 | + free(ptr); //for deallocating the memory |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + ptr=start; |
| 234 | + while(ptr->link!=NULL) |
| 235 | + { |
| 236 | + temp=ptr; |
| 237 | + ptr=ptr->link; |
| 238 | + } |
| 239 | + temp->link=NULL; //setting the link field of the second last element to null |
| 240 | + printf("\nThe deleted element is:%d\t",ptr->num); |
| 241 | + free(ptr); |
| 242 | + } |
| 243 | +} |
| 244 | +void delete_pos() //deleting a node from a specified position |
| 245 | +{ |
| 246 | + int i,pos; |
| 247 | + struct node *temp,*ptr; |
| 248 | + if(start==NULL) |
| 249 | + { |
| 250 | + printf("\nThe List is Empty:\n"); |
| 251 | + exit(0); |
| 252 | + } |
| 253 | + else |
| 254 | + { |
| 255 | + printf("\nEnter the position of the node to be deleted:\t"); |
| 256 | + scanf("%d",&pos); |
| 257 | + if(pos==0) //if the position is entered as beginning |
| 258 | + { |
| 259 | + ptr=start; |
| 260 | + start=start->link ; |
| 261 | + printf("\nThe deleted element is:%d\t",ptr->num ); |
| 262 | + free(ptr); |
| 263 | + } |
| 264 | + else |
| 265 | + { |
| 266 | + ptr=start; |
| 267 | + for(i=0;i<pos;i++) |
| 268 | + { |
| 269 | + temp=ptr; |
| 270 | + ptr=ptr->link ; |
| 271 | + if(ptr==NULL) |
| 272 | + { |
| 273 | + printf("\nPosition not Found:\n"); |
| 274 | + return; |
| 275 | + } |
| 276 | + } |
| 277 | + temp->link =ptr->link; //deleting the node |
| 278 | + printf("\nThe deleted element is:%d\t",ptr->num ); |
| 279 | + free(ptr); // FInal deallocation |
| 280 | + } |
| 281 | + } |
| 282 | +} |
0 commit comments