আপডেট

Wednesday, March 11, 2015

A complete program using link list

 #include <stdio.h>  
 #include <stdlib.h>  
 struct Node  
 {  
   int data;  
   struct Node* next;  
 };  
 typedef struct Node node;  
 node* head;  
 int main()  
 {  
 int p,i,n,a;;  
 while(1){  
   head==NULL;  
   int start;  
   start=instruction();  
 if(start==1){  
   insert_begining(10);  
   insert_begining(20);  
   insert_begining(30);  
   insert_begining(40);  
   insert_begining(50);  
   }  
  if(start==2){  
   insert_end(10);  
   insert_end(20);  
   insert_end(30);  
   insert_end(40);  
   insert_end(50);  
   }  
 else if(start==3){  
   printf("Enter position:");  
   scanf("%d",&p);  
   delete_node(p);  
 }  
 display();  
 }  
 return 0;  
 }  
 int instruction()  
 {  
   printf("\t1. Insert a node at beginning");  
   printf("\n\t2. Insert a node at end");  
   printf("\n\t3. delete a node");  
   printf("\n\n\tEnter your choice:");  
   int ins;  
   scanf("%d",&ins);  
   return ins;  
 }  
 void insert_begining(int data)  
 {  
   node* temp;  
   temp=(node*)malloc(sizeof(node));  
   temp->data=data;  
   temp->next=head;  
   head=temp;  
   }  
 void insert_end(int data)  
 {  
 node* temp;  
 if(head==NULL)  
 {  
   temp=(node*)malloc(sizeof(node));  
   temp->data=data;  
   temp->next=NULL;  
   head=temp;  
   return;  
 }  
 temp->next=(node*)malloc((sizeof(node)));  
 temp=temp->next;  
 temp->data=data;  
 temp->next=NULL;  
 }  
 void display()  
 {  
   node* temp;  
   temp=head;  
 printf("\nlist:");  
   while(temp!=NULL){  
     printf("\t%d ",temp->data);  
     temp=temp->next;  
   }  
   printf("\n\n");  
 }  
 void delete_node(int n){  
   node* temp1;  
   temp1=head;  
   if(head==NULL){  
     printf("List is empty\n");  
     return;  
   }  
   if(n==1){  
     head=head->next;  
     free(temp1);  
     return;  
   }  
 int i;  
 for(i=0;i<n-2;i++){  
   temp1=temp1->next;  
 }  
 node* temp2=temp1->next;  
 temp1->next=temp2->next;  
 free(temp2);  
 }