|
| 1 | +//Program to perform following operations on the singly linked list : |
| 2 | +//Inserting a node (at the start, at the end, in between), |
| 3 | +//deleting a node (starting node, last node, in between node), |
| 4 | +//displaying information stored in the nodes. |
| 5 | + |
| 6 | +#include<stdio.h> |
| 7 | +#include<stdlib.h> |
| 8 | +#include<stdbool.h> |
| 9 | + |
| 10 | +struct node{ |
| 11 | + int data; |
| 12 | + struct node *next; |
| 13 | +}; |
| 14 | + |
| 15 | +struct node *makenode(int makedata); |
| 16 | + |
| 17 | +struct node *insbeg(int newdata, struct node *first); |
| 18 | +void insend(int newdata, struct node *first); |
| 19 | +void insbet(int newdata, struct node *first); |
| 20 | + |
| 21 | +struct node *delbeg(struct node *first); |
| 22 | +void delend(struct node *first); |
| 23 | +void delbet(int deldata, struct node *first); |
| 24 | + |
| 25 | +void shownodes(struct node *first); |
| 26 | + |
| 27 | + |
| 28 | +int main(){ |
| 29 | + struct node *head = NULL; |
| 30 | + int dataf, choice, str = 1; |
| 31 | + |
| 32 | + if(head == NULL){ |
| 33 | + printf("Lets start a new list: \n"); |
| 34 | + printf("Enter data for node: "); |
| 35 | + scanf("%d", &dataf); |
| 36 | + |
| 37 | + head = (struct node *)malloc(sizeof(struct node)); |
| 38 | + head -> data = dataf; |
| 39 | + head -> next = NULL; |
| 40 | + } |
| 41 | + |
| 42 | +while(str == 1){ |
| 43 | + |
| 44 | + printf("\nEnter 1, 2 or 3 for Inserting a node at the start, at the end, in between.\n"); |
| 45 | + printf("Enter 4, 5 or 6 for deleting the starting node, last node, an in between node\n"); |
| 46 | + printf("Enter 7 for displaying information stored in the nodes."); |
| 47 | + |
| 48 | + scanf("%d", &choice); |
| 49 | + |
| 50 | + switch(choice){ |
| 51 | + case 1: |
| 52 | + printf("\nEnter the data for the new first node: "); |
| 53 | + scanf("%d", &dataf); |
| 54 | + head = insbeg(dataf, head); |
| 55 | + break; |
| 56 | + |
| 57 | + case 2: |
| 58 | + printf("\nEnter the data for the new end node: "); |
| 59 | + scanf("%d", &dataf); |
| 60 | + insend(dataf, head); |
| 61 | + break; |
| 62 | + |
| 63 | + case 3: |
| 64 | + printf("\nEnter the data for the new inbetween node: "); |
| 65 | + scanf("%d", &dataf); |
| 66 | + insbet(dataf, head); |
| 67 | + break; |
| 68 | + |
| 69 | + case 4: |
| 70 | + printf("\nDeleting the starting node..."); |
| 71 | + head = delbeg(head); |
| 72 | + break; |
| 73 | + |
| 74 | + case 5: |
| 75 | + printf("\nDeleting the ending node..."); |
| 76 | + delend(head); |
| 77 | + break; |
| 78 | + |
| 79 | + case 6: |
| 80 | + printf("\nEnter the data for the node which is to be deleted:"); |
| 81 | + scanf("%d", &dataf); |
| 82 | + delbet(dataf, head); |
| 83 | + break; |
| 84 | + |
| 85 | + case 7: |
| 86 | + printf("\nShowing all the nodes with data: "); |
| 87 | + shownodes(head); |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | +printf("\nWant to continue: 1- yes, 0- no\n"); |
| 92 | +scanf("%d", &str); |
| 93 | + |
| 94 | +} |
| 95 | + return 0; |
| 96 | +} |
| 97 | + |
| 98 | +struct node *makenode(int makedata){ |
| 99 | + struct node *ptr; |
| 100 | + ptr = (struct node *)malloc(sizeof(struct node)); |
| 101 | + |
| 102 | + if(ptr == NULL){ |
| 103 | + printf("\nMemory not available."); |
| 104 | + exit(1); |
| 105 | + } |
| 106 | + |
| 107 | + ptr -> data = makedata; |
| 108 | + ptr -> next = NULL; |
| 109 | + return ptr; |
| 110 | +} |
| 111 | + |
| 112 | +struct node *insbeg(int newdata, struct node *first){ |
| 113 | + struct node *temp; |
| 114 | + |
| 115 | + temp = makenode(newdata); |
| 116 | + |
| 117 | + temp -> next = first; |
| 118 | + first = temp; |
| 119 | + |
| 120 | + return first; |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +void insend(int newdata, struct node *first){ |
| 125 | + struct node *temp, *q; |
| 126 | + q = first; |
| 127 | + |
| 128 | + temp = makenode(newdata); |
| 129 | + |
| 130 | + while(q != NULL){ |
| 131 | + |
| 132 | + if( q -> next == NULL ){ |
| 133 | + q-> next = temp; |
| 134 | + break; |
| 135 | + } |
| 136 | + q = q -> next; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +void insbet(int newdata, struct node *first){ |
| 141 | + int nodeval; |
| 142 | + bool b = false; |
| 143 | + struct node *temp, *q; |
| 144 | + q = first; |
| 145 | + |
| 146 | + temp = makenode(newdata); |
| 147 | + |
| 148 | + printf("\nSo where do you want to insert the node? (prev node value after which it will be inserted)"); |
| 149 | + scanf("%d", &nodeval); |
| 150 | + |
| 151 | + while(q -> data != nodeval){ |
| 152 | + |
| 153 | + if(q -> next -> data == nodeval){ |
| 154 | + b = true; |
| 155 | + temp -> next = q -> next -> next; |
| 156 | + q -> next -> next = temp; |
| 157 | + } |
| 158 | + q = q -> next; |
| 159 | + } |
| 160 | + |
| 161 | + if(b == false){ |
| 162 | + printf("The value you entered doesnt exist."); |
| 163 | + } |
| 164 | + |
| 165 | +} |
| 166 | +// del |
| 167 | + |
| 168 | +struct node *delbeg(struct node *first){ |
| 169 | + struct node *q, *r; |
| 170 | + q = first -> next; |
| 171 | + r = first; |
| 172 | + |
| 173 | + free(r); |
| 174 | + r = NULL; |
| 175 | + |
| 176 | + return q; |
| 177 | +} |
| 178 | + |
| 179 | +void delend(struct node *first){ |
| 180 | + struct node *q, *r; |
| 181 | + q = first; |
| 182 | + |
| 183 | + while(q->next != NULL){ |
| 184 | + if(q -> next -> next == NULL){ |
| 185 | + r = q -> next; |
| 186 | + q -> next = NULL; |
| 187 | + free(r); |
| 188 | + r = NULL; |
| 189 | + break; |
| 190 | + } |
| 191 | + q = q-> next; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +void delbet(int deldata, struct node *first){ |
| 196 | + struct node *q, *r; |
| 197 | + q = first; |
| 198 | + |
| 199 | + while(q -> data != deldata){ |
| 200 | + if(q -> next -> data == deldata){ |
| 201 | + r = q -> next; |
| 202 | + q-> next = r -> next; |
| 203 | + free(r); |
| 204 | + r = NULL; |
| 205 | + break; |
| 206 | + } |
| 207 | + q = q -> next; |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +void shownodes(struct node *first){ |
| 212 | + struct node *q; |
| 213 | + q = first; |
| 214 | + printf("\n"); |
| 215 | + |
| 216 | + while(q != NULL){ |
| 217 | + printf("%d -> ", q -> data); |
| 218 | + q = q -> next; |
| 219 | + } |
| 220 | + |
| 221 | +} |
0 commit comments