This is a C Program to perform list traversal operation using linked list. This C program to show the basic functions of linked list such as add, delete, append and delete.
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | #include "stdio.h" #include "alloc.h" #include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> create (); void append(); void add(); void del(); void print(); typedef struct s_list { int val; struct s_list *next; }*SLL; SLL start,last,temp,extra,one; void checkme(); //check if list is empty if so automatically create it... void checkme() { temp=start; if(temp==NULL) //start==null then goto create { printf("\n\t\t\t\t Entering creating mode..."); create(); //okay... } } // main start here with no arguments... main() { int choice; clrscr(); start=NULL; // always make start node NULL MENU: printf("\n MENU\n\n 1: create\n 2: append\n 3: print\n 4: add\n 5: delete \n 6: exit :_____\b\b\b\b"); scanf("%d",&choice); switch(choice) { case 1: create(); break; case 2: append(); break; case 3: print(); break; case 4: add(); break; case 5: del(); break; case 6: exit(0); default: printf("\n choice failed...");break; } goto MENU; } create() { if(start==NULL) { start= malloc(sizeof(struct s_list)); //alloc the struct s_list printf("\n\t\t\t\t input initilal value:"); scanf("%d",&start->val); // get init value start->next=NULL; // if you fail to mke this null...garbage list is placed } else { printf("\n\t\t\t\t already created..."); } return; } void append() { temp=start; // always give starting of list to temporary // so that we can make changes... if(temp==NULL) { printf("\n\t\t\t\t entering creating mode..."); create(); } else { last=malloc(sizeof(struct s_list)); if(last==NULL){printf("\n\t\t\t\t not enough memory...");} while(temp->next!=NULL) temp=temp->next; printf("\n\t\t\t\t N enter value : "); scanf("%d",&last->val); last->next=NULL; temp->next=last; } return; } void print() { checkme(); // always check for list? created or not? temp=start; printf("\n List ::=>"); while(temp!=NULL) { printf("\t%d",temp->val); temp=temp->next; } return; } void add() { int data; checkme(); temp=start; printf("\n\t\t\t\t enter before which :"); scanf("%d",&data); if(data==temp->val) { printf("\t\t\t\tstart val..."); last=malloc(sizeof(struct s_list)); printf("\n\t\t\t\t get the value...:"); scanf("%d",&last->val); last->next=temp; start=last; return; //getch(); } while(temp!=NULL) //go upto last node { if(temp->val==data) //if found { printf("\n\t\t\t\t making changes..."); extra=temp; //point to that node one=temp; // here i used two ptr "extra and one" break; //break the loop and work on the same } temp=temp->next; printf("\n\t\t\t\t\t not found.."); // return; } temp=start; // set temp to start while(temp->next!=extra) //now find upto pointed value or node { printf("%d",temp->val); temp=temp->next; } last=malloc(sizeof(struct s_list)); printf("\n\t\t\t\t value : "); scanf("%d",&last->val); // printf("temp-> %d *temp-> %d",temp,*temp); temp->next=last; // join prev+new last->next=one; // join prev+new+remaning // return; } void del() { int data;//,cnt=0; checkme(); temp=start; printf("\n enter number to be deleted :"); scanf("%d",&data); while(temp!=NULL) //temp->next for next and temp for the given number { //cnt+=1; if(temp->next->val==data) //if you remove ->next next to next value is deleted { printf("\n\t\t\t\t making changes..."); extra=temp->next; //take the link to be deleted temp->next=temp->next->next; // join the deleted free(extra); // finally librate the link.. break; } // printf("%d\n",cnt); temp=temp->next; //printf("\n\t\t\t\t\t not found..cnt=%d",cnt); // return; } return; } |