Link List

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct lnklist
{
	int data;
   struct lnklist *next;                 
};
typedef struct lnklist NODE ;
NODE *start=NULL;
int n,q;
void insert();
void display();
void delet();
NODE*  reverse();

int main (void)
{
	int ch=0;
   while(ch!=5)
   {
   	printf("\nEnter your choise:\n1. Insert\n2. Display\n3. Delete \n4. Reverse\n5. Exit\n");
   	scanf("%d",&ch);
      switch(ch)
      {
      	case 1: insert();
         	break;
         case 2: display();
         	break;
         case 3: delet();
         	break;
         case 4: start=reverse();
         	break;
      }
   }
   return 0;
}


void insert()
{
	NODE *p,*node;
   p=(NODE*)malloc(sizeof(NODE));
   if(p==NULL)
   	printf("NO Space!");
   else
   {
   	printf("Enter the element in node: ");
      scanf("%d",&p->data);
      p->next=NULL;
      if(start==NULL)
      	start=p;
      else
      {
         node=start;
         while(node->next!=NULL)
         {
      		node=node->next;
         }
         node->next=p;
      }
   }
   clrscr();
}



void display()
{
	NODE *node;
   if(start==NULL)
   	printf("List is Empty\n\n");
   else
   {
      printf("The elements are: ");
   	node=start;
      while(node!=NULL)
      {
      	printf("%d ",node->data);
         node=node->next;
      }
   }
   getch();
   clrscr();
}

void delet()
{
	NODE *node,*p;
   int pos,i;
   if(start==NULL)
   	printf("List is Empty! Deletion can't possible!\n\n");
   else
   {
      node=start;
   	printf("Enter the position: ");
   	scanf("%d",&pos);
      if(pos==1)
      {
      	start=node->next;
         free(node);
      }
      else
      {
      	for(i=1;i<pos;i++)
      	{
      		p=node;
      		node=node->next;
     		}
      	p->next=node->next;
         free(node);
      }
   }
}

NODE* reverse()
{
	NODE *p,*q,*node;
   {
     	q=NULL;
      node=start;
      while(node!=NULL)
      {
      	p=node;
         node=node->next;
         p->next=q;
         q=p;
      }
      return q;
   }
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *