Perform the Implementation of Single Link List
Source Code for Single Link list in C and C++ Language with the following functions ::
- Create Single Link list
- Display Single Link list
- Insert Node
- Delete node
- Reverse Single Link list
- Revert
Single link list Assignment is implemented using pointers with every function is divided modular to increase codes readability .
Single Link List C Language code
#include #include typedef struct node { int info; struct node *lnk; }node; node* create(); node* alloc(); void display(node *); node* insert(node *,int); node* insert1(node *,int); node* dlt(node *,int); node* reverse(node *); void main() { int ch,n,a[10],key,ch1; node *f,*f1,*f2; do { clrscr(); printf("\t\t\tLINKED LIST MAIN MENU\n"); printf("\t\t\t1:CREATE\n"); printf("\t\t\t2:DISPLAY\n"); printf("\t\t\t3:INSERT\n"); printf("\t\t\t4:DELETE\n"); printf("\t\t\t5:REVERSE\n"); printf("\t\t\t6:REVERT\n"); printf("\t\t\t7:EXIT\n"); printf("\t\tENTER YOUR CHOICE\n"); scanf("%d",&ch); switch(ch) { case 1:clrscr(); f=create(); getch(); break; case 2:clrscr(); printf("\n\t\tDISPLAYING THE LINKED LIST\n\n"); display(f); getch(); break; case 3:clrscr(); do { clrscr(); printf("\n\t\tINSERT MENU"); printf("\n\t\t1.BEFORE A NO\n\t\t2.AFTER A NO\n\t\t3.EXIT"); scanf("%d",&ch1); switch(ch1) { case 1: clrscr(); printf("ENTER THE ELEMENT OF THE LIST"); scanf("%d",&key); f=insert(f,key); display(f); getch(); break; case 2: clrscr(); printf("ENTER THE ELEMENT OF THE LIST"); scanf("%d",&key); f1=insert1(f,key); display(f1); getch(); break; case 3: break; } }while(ch1!=3); break; case 4: clrscr(); printf("ENTER THE ELEMENT OF THE LIST"); scanf("%d",&key); f1=dlt(f,key); display(f1); getch(); break; case 5: f2=reverse(f); display(f2); f2=reverse(f2); getch(); break; case 6: f=reverse(f); display(f); printf("\n\t\tLIST REVERSED\t\t"); getch(); break; case 7: break; default: printf("WRONG CHOICE !!!! RE ENTER"); getch(); break; } }while(ch!=7); } node* create() { node *f,*nw,*lst; f=NULL; do { nw=alloc(); if(f==NULL) f=nw; else lst->lnk=nw; lst=nw; printf("\n\n\tDO YOU WANT TO ENTER ANOTHER DATABASE(y/n)?"); }while(getche()=='y'); return f; } node* alloc() { node *f; f=(node *)malloc(sizeof(node)); f->lnk=NULL; printf("\n\tENTER THE ELEMENT TO BE ADDED"); scanf("%d",&f->info); return f; } void display(node *f) { if(f==NULL) { printf("\n\n\t\tLIST NOT PRESENT !!!!"); return; } printf("\n\tINFO\tRPTR\tADD\n"); while(f!=NULL) { printf("\t%d\t%d\t%d\n",f->info,f->lnk,f); f=f->lnk; } } node* insert(node *f,int key) { node *nw,*t; if(f->info==key) { nw=alloc(); nw->lnk=f; f=nw; return f; } while(f->lnk!=NULL) { if(f->lnk->info==key) { nw=alloc(); nw->lnk=f->lnk; f->lnk=nw; return f; } f=f->lnk; } printf("\n\t\t\tELEMENT NOT FOUND !!!!"); return f; } node* insert1(node *f,int key) { node *nw,*p; f=p; while(p!=NULL) { if(key==p->info) { nw=alloc(); nw->lnk=p->lnk; p->lnk=nw; return f; } p=p->lnk; } printf("element not found"); } node* dlt(node *f,int key) { node *p,*q; p=f; if(f==NULL) { printf("\n\tLINKED LIST EMPTY"); return f; } if(key==f->info) { f=f->lnk; free(p); return f; } else { while(p->lnk!=NULL) { if(key==(p->lnk)->info) { q=p->lnk; p->lnk=p->lnk->lnk; free(q); } p=p->lnk; } return f; } printf("ELEMENT NOT FOUND"); } node* reverse(node *f) { node *p,*t,*r; p=NULL; t=f; r=t->lnk; while(t!=NULL) { t->lnk=p; p=t; t=r; if(t!=NULL) r=r->lnk; } return(p); }
Leave a Reply