Вы находитесь на странице: 1из 9

Insertion in a linked list

printf("enter the element after which u want to insert the node"); scanf("%d",&num); ptr=start; while(ptr!=NULL) { if((num!=ptr->info)&&(ptr!=NULL)) { ptr=ptr->next; }
else if((num==ptr->info)&&(ptr!=NULL)) { n=(nodetype *)malloc(sizeof(nodetype)); printf("info u want to insert"); scanf("%d",&item);

n->info=item; n->next=ptr->next; ptr->next=n; ptr=n; printf("node is being inserted with info %d",ptr->info); break; } if(ptr==NULL) { printf("element is not present"); } getch();

Deletion in a linked list


ptr=start; Printf(enter the item which u want to delete); Scanf(%d,&item); If(start==NULL) { printf("list is empty and Item not in the list"); break; } else if(start->info==item) { save=NULL; ptr=start; start=ptr->next; free(ptr); Break; }

while(ptr!=NULL) { if((item!=ptr->info)&&(ptr!=NULL)) { save=ptr; ptr=save->next; } else if((item==ptr->info)&&(ptr!=NULL)) { save->next=ptr->next; free(ptr); break; }

Creation of 2 way link list


typedef struct node { int info; struct node *next; struct node *prev; }nodetype; void main() { char opt; nodetype *r,*l,*start=NULL,*n; int num; clrscr(); do{ printf("\nEnter the info u want to enter"); scanf("%d",&num);

n=(nodetype *)malloc(sizeof(nodetype)); if(start==NULL) { start=n; r=n; l=n; l->info=num; l->next=NULL; l->prev=NULL; } else { l->next=n; l=n; l->info=num; l->next=NULL; l->prev=r; r=n; }

printf("\nDo you want to add new node");


opt=getch(); }while((opt=='y')&&(ptr!=NULL)); getch(); }

INFO[PTR] LINK[PTR]

Ptr->info Ptr->next

PTR=LINK[PTR]
INFO[PTR]=NUM PTR= NEW LINK[PTR]=NEW

Ptr=ptr->next
Ptr->info = num Ptr=new Ptr->next=new

LINK[NEW]=LINK[SAVE New->next=save->next ]

Вам также может понравиться