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.
#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; }