Archive for the ‘Linked list’ Category


#include<stdio.h>
#include<stdlib.h>

struct circular
{
    int i;
    struct circular *front;
    struct circular *back;
};


struct circular *temp;
struct circular *head;
struct circular *p;
struct circular *mid;
struct circular *move;

int cnt=0;


void create(void);
void insert(void);
void display(void);
void del(void);

int main()
{
    int ch=0;
    while(ch!=5)
    {
        printf("\n1.CREATE");
        printf("\n2.INSERT");
        printf("\n3.DELETE");
        printf("\n4.DISPLAY");
        printf("\n5.EXIT\n");
        scanf("%d",&ch);


        if(ch==1)
        {
            create();
            cnt++;
            cnt++;
        }

        if(ch==2)
        {
            insert();
            cnt++;
        }
        if(ch==3)
        {
            del();
            cnt--;
        }

        if(ch==4)
        {
            display();
        }

        if(ch==5)
        {
            break;
        }
    }
}
void create()
{

	
    head=(struct circular *)malloc(sizeof(struct circular));
    head->back=head;
    head->front=head;

    printf("ENETER THE DATA\n");
    scanf("%d",&head->i);
    temp=head;

    temp->back=(struct circular *)malloc(sizeof(struct circular));
    temp=temp->back;
    temp->back=head;
    head->front=temp;
printf("ENETER THE DATA\n");
    scanf("%d",&temp->i);


    
}
void insert()
{
    int add,t;

    printf("\n\t ENTER ANY NUMBER BETWEEN 1 AND %d\n",cnt);
    scanf("%d",&add);
    p=head;
    t=1;
    while(t<add)
    {
        p=p->back;
        t++;
    }
    mid=(struct circular *)malloc(sizeof(struct circular));
    printf("\n\n\nENETER THE DATA\n");
    scanf("%d",&mid->i);

    mid->back=p->back;
    p->back=mid;
    p->back->front=mid;
    mid->front=p;

}
void display()
{
    p=head;
    printf("\n%d ",p->i);
    p=p->back;
    while(p!=head)
    {
        printf("%d ",p->i);
        p=p->back;
    }
	printf("\n");

}

void del(void)
{
    int add,t;

    printf("\n\n\t ENTER ANY NUMBER BETWEEN 1 AND %d\n",cnt);
    scanf("%d",&add);
    p=head;
    t=1;
    while(t<add-1)
    {
        p=p->back;
        t++;
    }
   
    mid=p->back;
    p->back=mid->back;
    mid->back->front=p;
    free(mid);
}
Advertisements
#include <stdio.h>
#include <stdlib.h>
       void  create_list(int);
      void addatbeg(int);
void       del(int);
     void addafter(int,int);
void       display(void);
void       count(void);
void       rev(void);
struct node
{
  struct node *prev;
  int info;
  struct node *next;
}*start;
 
 int main()
{
  int choice=1,n,m,po,i;
  start=NULL;
  while(choice!=8)
  {
    printf("1.Create List\n");
    printf("2.Add at begining\n");
    printf("3.Add after\n");
    printf("4.Delete\n");
    printf("5.Display\n");
    printf("6.Count\n");
    printf("7.Reverse\n");
    printf("8.exit\n");
    printf("Enter your choice : ");
    scanf("%d",&choice);
    switch(choice)
    {
     case 1:
      printf("How many nodes you want : ");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
        printf("Enter the element : ");
        scanf("%d",&m);
        create_list(m);
      }
      break;
     case 2:
      printf("Enter the element : ");
      scanf("%d",&m);
      addatbeg(m);
      break;
     case 3:
      printf("Enter the element : ");
      scanf("%d",&m);
      printf("Enter the position after which this element is inserted : ");
      scanf("%d",&po);
      addafter(m,po);
      break;
     case 4:
      printf("Enter the element for deletion : ");
      scanf("%d",&m);
      del(m);
      break;
     case 5:
      display();
      break;
     case 6:
      count();
      break;
     case 7:
      rev();
      break;
     case 8:
				break;
     default:
      printf("Wrong choice\n");
  }/*End of switch*/
   }/*End of while*/
}/*End of main()*/
 
void create_list(int num)
{
struct node *q,*tmp;
tmp= (struct node *) malloc(sizeof(struct node));

tmp->info=num;
tmp->next=NULL;
if(start==NULL)
{
printf("Creating the first node of Linked List \n");
tmp->prev=NULL;
//start->prev=tmp;
start=tmp;
}
else
{
printf("Inserting the nodes after the head node/other nodes\n");
q=start;
while(q->next!=NULL)
q=q->next;
q->next=tmp;
tmp->prev=q;
}
}/*End of create_list()*/ 
void addatbeg(int num)
{
  struct node *tmp;
  tmp=malloc(sizeof(struct node));
  tmp->prev=NULL;
  tmp->info=num;
  tmp->next=start;
  start->prev=tmp;
  start=tmp;
}/*End of addatbeg()*/
 
void addafter(int num,int c)
{
  struct node *tmp,*q;
  int i;
  q=start;
  for(i=0;i<c-1;i++)
  {
    q=q->next;
    if(q==NULL)
    {
      printf("There are less than %d elements\n",c);
      return;
    }
  }
  tmp=malloc(sizeof(struct node) );
  tmp->info=num;
  q->next->prev=tmp;
  tmp->next=q->next;
  tmp->prev=q;
  q->next=tmp;
}/*End of addafter() */
 
void del(int num)
{
  struct node *tmp,*q;
  if(start->info==num)
  {
    tmp=start;
    start=start->next;  /*first element deleted*/
    start->prev = NULL;
    free(tmp);
    return;
  }
  q=start;
  while(q->next->next!=NULL)
  {
    if(q->next->info==num)     /*Element deleted in between*/
    {
      tmp=q->next;
      q->next=tmp->next;
      tmp->next->prev=q;
      free(tmp);
      return;
    }
    q=q->next;
  }
  if(q->next->info==num)    /*last element deleted*/
  {   tmp=q->next;
    free(tmp);
    q->next=NULL;
    return;
  }
  printf("Element %d not found\n",num);
}/*End of del()*/
 
void display()
{
  struct node *q;
  if(start==NULL)
  {
    printf("List is empty\n");
    return;
  }
  q=start;
  printf("List is :\n");
  while(q!=NULL)
  {
    printf("%d ", q->info);
    q=q->next;
  }
  printf("\n");
}/*End of display() */
 
void count()
{   struct node *q=start;
  int cnt=0;
  while(q!=NULL)
  {
    q=q->next;
    cnt++;
  }
  printf("Number of elements are %d\n",cnt);
}/*End of count()*/
 
void rev(void)
{
  struct node *p1,*p2;
  p1=start;
  p2=p1->next;
  p1->next=NULL;
  p1->prev=p2;
  while(p2!=NULL)
  {
    p2->prev=p2->next;
    p2->next=p1;
    p1=p2;
    p2=p2->prev; /*next of p2 changed to prev */
  }
  start=p1;
}/*End of rev()*/
// Program of circular linked list
#include <stdio.h>
#include <stdlib.h>
 void create_list(int);
void   addatbeg(int);
void       addafter(int,int);
void  del(int);
void display(void);
struct node
{
  int info;
  struct node *link;
}*last;
 
int main()
{
  int choice=1,n,m,po,i;
  last=NULL;
  while(choice!=6)
  {
    printf("1.Create List\n");
    printf("2.Add at begining\n");
    printf("3.Add after \n");
    printf("4.Delete\n");
    printf("5.Display\n");
    printf("6.Quit\n");
    printf("Enter your choice : ");
    scanf("%d",&choice);
 
    switch(choice)
    {
     case 1:
      printf("How many nodes you want : ");
      scanf("%d",&n);
      for(i=0; i < n;i++)
      {
        printf("Enter the element : ");
        scanf("%d",&m);
        create_list(m);
      }
      break;
     case 2:
      printf("Enter the element : ");
      scanf("%d",&m);
      addatbeg(m);
      break;
     case 3:
      printf("Enter the element : ");
      scanf("%d",&m);
      printf("Enter the position after which this element is inserted : ");
      scanf("%d",&po);
      addafter(m,po);
      break;
     case 4:
      if(last == NULL)
      {
        printf("List underflow\n");
        continue;
      }
      printf("Enter the number for deletion : ");
      scanf("%d",&m);
      del(m);
      break;
     case 5:
      display();
      break;
     case 6:
			{
				break;
			}
     default:
      printf("Wrong choice\n");
    }/*End of switch*/
  }/*End of while*/
}/*End of main()*/
 
void create_list(int num)
{
  struct node *tmp;
  tmp= malloc(sizeof(struct node));
  tmp->info = num;
 
  if(last == NULL)
  {
    last = tmp;
    tmp->link = last;
  }
  else
  {
    tmp->link = last->link; /*added at the end of list*/
    last->link = tmp;
    last = tmp;
  }
}/*End of create_list()*/
 
void addatbeg(int num)
{
  struct node *tmp;
  tmp = malloc(sizeof(struct node));
  tmp->info = num;
  tmp->link = last->link;
  last->link = tmp;
}/*End of addatbeg()*/
 
void addafter(int num,int pos)
{
 
  struct node *tmp,*q;
  int i;
  q = last->link;
  for(i=0; i < pos-1; i++)
  {
    q = q->link;
    if(q == last->link)
    {
      printf("There are less than %d elements\n",pos);
      return;
    }
  }/*End of for*/
  tmp = malloc(sizeof(struct node) );
  tmp->link = q->link;
  tmp->info = num;
  q->link = tmp;
  if(q==last)    /*Element inserted at the end*/
    last=tmp;
}/*End of addafter()*/
 
void del(int num)
{
  struct node *tmp,*q;
  if( last->link == last && last->info == num)  /*Only one element*/
  {
    tmp = last;
    last = NULL;
    free(tmp);

  }
  q = last->link;
  if(q->info == num)
  {
    tmp = q;
    last->link = q->link;
    free(tmp);
    return;
  }
  while(q->link != last)
  {
    if(q->link->info == num)     /*Element deleted in between*/
    {
      tmp = q->link;
      q->link = tmp->link;
      free(tmp);
      printf("%d deleted\n",num);
      return;
    }
    q = q->link;
  }/*End of while*/
  if(q->link->info == num)    /*Last element deleted q->link=last*/
  {
    tmp = q->link;
    q->link = last->link;
    free(tmp);
    last = q;
    return;
  }
  printf("Element %d not found\n",num);
}/*End of del()*/
 
void display()
{
  struct node *q;
  if(last == NULL)
  {
    printf("List is empty\n");
    return;
  }
  q = last->link;
  printf("List is :\n");
  while(q != last)
  {
    printf("%d ", q->info);
    q = q->link;
  }
  printf("%d\n",last->info);
}/*End of display()*/
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
struct node
{ int data;
struct node *link;
};
void append(struct node **,int);
void in_begin(struct node **,int);
void del(struct node **,int);
void in_middle(struct node **,int,int);
int count(struct node *);
void display(struct node *);
 
int main()
{ struct node *p; /* p can be said as the head or a start ptr */
p=NULL;
/* Printing the menu */
int num,loc;
char choice;
	while(1)
	{
		fflush(stdin);
printf("\nPROGRAM TO IMPLEMENT SINGLY LINKED LIST ");
printf("\n=====================================");
printf("\n\n1.Create \\ Appending The List");
printf("\n2.Insert Node At Begining");
printf("\n3.Insert Node In Middle");
printf("\n4.Deleting a Node");
printf("\n5.Counting The No Of Nodes");
printf("\n6.Displaying the list");
printf("\n7.Exit");
printf("\n\nEnter ur Choice : ");
scanf("%c",&choice);
switch(choice)
{
 case '1':
			{
char ans;
				int a=1;
				while(a)
				{
printf("Enter any number : ");
scanf("%d",&num);
append(&p,num);
					fflush(stdin);
printf("Enter more (y/n) :");
scanf("%c",&ans);
					if(ans=='n')
						a=0;
				}

break;
			}
case '2':
printf("Enter The Data : ");
scanf("%d",&num);
in_begin(&p,num);
break;
 
case '3':
printf("\nEnter The Position :");
scanf("%d",&loc);
printf("\nEnter The Data : ");
scanf("%d",&num);
in_middle(&p,loc,num);
break;
 
case '4':
printf("\nEnter The Data u Want To Delete : ");
scanf("%d",&num);
del(&p,num);
break;
 
case '5':
printf("\nThe No Of Nodes Are %d",count(p));
break;
 
case '6':
display(p);
break;
 
case '7':
printf("\n\nQuiting.......");
break;
 
default:
printf("Invalid choice.Please Enter Correct Choice\n");

 
}
 
}
	return 0;
 
}
 
void append(struct node **q,int num)
{ struct node *temp,*r;
temp = *q;
if(*q==NULL)
{ temp = (struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{ temp = *q;
while(temp->link !=NULL)
{ temp=temp->link;
}
r = (struct node *)malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
}
 
void display(struct node *q)
{ if(q==NULL)
{ printf("\n\nEmpty Link List.Can't Display The Data\n\n");
}
	else
while(q!=NULL)
{ printf("\n%d\n",q->data);
q=q->link;
}

}
 
int count(struct node *q)
{ int c=0;
if(q==NULL)
{ printf("Empty Link List.\n");

goto last;
}
while(q!=NULL)
{ c++;
q=q->link;
}
last:
return c;
 
}
 
void in_begin(struct node **q,int num)
{ struct node *temp;
if(*q==NULL)
{ printf("Link List Is Empty.Can't Insert.\n");
}
else
{ temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp; /* pointing to the first node */
}
}
 
void in_middle(struct node **q,int loc,int num)
{ struct node *temp,*n;
int c=1,flag=0;
temp=*q;
if(*q==NULL)
{ printf("\n\nLink List Is Empty.Can't Insert.\n");
}
else
while(temp!=NULL)
{ if(c==loc)
{ n = (struct node *)malloc(sizeof(struct node));
n->data=num;
n->link=temp->link;
temp->link=n;
flag=1;
}
c++;
temp=temp->link;
}
if(flag==0)
{ printf("\n\nNode Specified Doesn't Exist.Cant Enter The Data");

}
else
{ printf("Data Inserted");

}
}
 
void del(struct node**q,int num)
{ if(*q==NULL)
{ printf("\n\nEmpty Linked List.Cant Delete The Data.\n\n");

}
else
{
struct node *old,*temp;

		old=(struct node*)malloc(sizeof(struct node));
int flag=0;
temp=*q;
while(temp!=NULL)
{ if(temp->data==num)
{ if(temp==*q) /* First Node case */
*q=temp->link; /* shifted the header node */
else
old->link=temp->link;
 
free(temp);
flag=1;
				break;
}
else
{ 
				old=temp;

temp=temp->link;
}
}
if(flag==0)
printf("\nData Not Found...\n");
else
printf("\nData Deleted...");

}

}