Thursday, January 29, 2015

dqueue using linkedlist

#include< stdio.h>

typedef struct dqueue
{
 int data;
 struct dqueue *left,*right;
}list;

list *front, *rear;

int choice,element;

list *add_front(list *);
list *add_rear(list *);
list *delete_front(list *);
list *delete_rear(list *);
void display(list *,list *);
int count(list *);



void main()
{
 front=(list *)malloc(sizeof(list));
 rear=(list *)malloc(sizeof(list));

 front->left=NULL;
 rear->right=NULL;

 front->right=rear;
 rear->left=front;

 while(1)
 {
  choice=menu();
  switch(choice)
  {
   case 1: front=add_front(front);   break;
   case 2: rear=add_rear(rear);   break;
   case 3: front=delete_front(front);break;
   case 4: rear=delete_rear(rear);   break;
   case 5: display(front,rear); getch();break;
   case 6: printf("\nno of items in dqueue : %d\n",count(front));getch();break;
   case 7: exit();
   default : printf("\nInvalid option...\n");getch();
  }
 }
}

int menu()
{
 clrscr();
 printf("Queue elements : \n\n");
 display(front,rear);
 printf("\n\nMENU\n---------------------\n1. add item at front\n2. add item at back");
 printf("\n3. delete item from front\n4. delete item from rear\n5. display items\n6. count items\n7. exit");

 printf("\nenter your choice : ");
 scanf("%d",&choice);
 return choice;
}

list *add_front(list *front)
{
 list *temp;
 temp=(list *)malloc(sizeof(list));
 
 if(temp==NULL)
 {
  printf("\nInsufficient Memory...\n");
  getch();
  return;
 }
 else
 {
  printf("\nenter element to add at front : ");
  scanf("%d",&element);
  front->data=element;
  front->left=temp;
  temp->right=front;
  temp->left=NULL;
  front=temp;
 }
 return (front);

}

list *add_rear(list *rear)
{
 list *temp;
 temp=(list *)malloc(sizeof(list));

 if(temp==NULL)
 {
  printf("\nInsufficient Memory...\n");
  getch();
  return;
 }
 else
 {
  printf("\nenter element to add at rear : ");
  scanf("%d",&element);
  rear->data=element;
  rear->right=temp;
  temp->left=rear;
  temp->right=NULL;
  rear=temp;
 }
 return (rear);

}

list *delete_front(list *front)
{
 list *temp;
 temp=front->right->right;

 free(front->right);
 front->right=temp;
 front->right->left=front;
 return (front);
}

list *delete_rear(list *rear)
{
 list *temp;
 temp=rear->left->left;

 free(rear->left);
 rear->left=temp;
 rear->left->right=rear;
 return (rear);
}

void display(list *front,list *rear)
{
 list *temp;
 if(count(front)==0)
 {
  printf("\n<---- Dqueue Empty ---->\n");
  return;
 }
 else
 {
  printf("displaying left -> right :\n");
  temp=front->right;
  while(temp->right!=NULL)
  { printf("%d ",temp->data);temp=temp->right;}
  printf("\ndisplaying left <- right :\n");
  temp=rear->left;
  while(temp->left!=NULL)
  { printf("%d ",temp->data);temp=temp->left;}
 }
}

int count(list *front)
{
 if(front->right->right==NULL)
  return 0;
 else
  return (1+count(front->right));
}

Monday, April 2, 2012

cqueue using linkedlist


#include< stdio.h>

typedef struct cqueue
{
	int data;
	struct cqueue *next;
}list;

list *front,*rear;

list *push(list *);
list *pop(list *);
void display(list *);
int count(list *);
int menu(list *);

void main()
{
	int choice;

	front=(list *)malloc(sizeof(list));
	rear=(list *)malloc(sizeof(list));
	front->next=rear;
	rear->next=front;
	front->data=rear->data=NULL;

	while(1)
	{
		choice=menu(front);
		switch(choice)
		{
			case 1: rear=push(rear);break;
			case 2: front=pop(front);break;
			case 3:	display(front);getch();break;
			case 4: printf("\nno. of elements in quque : %d",count(front));getch();break;
			case 5: exit();
			default : printf("\nInvalid option\n");getch();
		}
	}
}

int menu(list *front)
{
	int choice;
	clrscr();
	printf("\nQueue elements\n\n");
	display(front);
	printf("\n\nMENU\n--------------\n1. add item\n2. delete item\n3. display items\n4. count items\n5. exit\n");
	printf("enter your choice : ");
	scanf("%d",&choice);
	return(choice);
}


list *push(list *rear)
{
	list *temp;
	int element;
	temp=(list *)malloc(sizeof(list));
	if(temp==NULL)
	{
		printf("\ninsufficient memory..\n");
		getch();
		exit();
	}
	else
	{
		printf("\nenter element to add : ");
		scanf("%d",&element);
		temp->data=element;
		rear->next=temp;
		temp->next=front;
		rear=temp;
	}
	return (rear);
}

list *pop(list *front)
{
	list *temp;

	if(front->next!=rear)
	{
		temp=front->next;
		free(front);
		front=temp;
	}
	else
	{
		printf("\nqueue empty\n");
		getch();
		return (front);
	}
	return (front);
}

int count(list *front)
{
	list *temp;
	temp=front;

	if(temp->next==rear)
		return 0;
	else
		return (1+count(temp->next));
}

void display(list *front)
{
	list *temp;
	temp=front->next;
	if(front->next==rear)
	{
		printf("\nqueue empty\n");
		return;
	}
	else
	{
		while(temp!=rear)
		{
			printf("%d ",temp->next->data);
			temp=temp->next;
		}
	}

}
	
		

cqueue using array


#include< stdio.h>

int queue[10],front,rear,element,max=10,choice;

void push();
void pop();
void display();
int menu();
int count();

void main()
{
	front=0;
	rear=-1;
	while(1)
	{
		choice=menu();
		switch(choice)
		{
			case 1: push();
				getch();
				break;
			case 2: pop();
				getch();
				break;
			case 3: printf("\ndisplaying queue items... \n");
				display();
				getch();
				break;
			case 4: printf("\nno.of elements in queue : %d\n",count());
				getch();
				break;
			case 5:	exit();
			default: printf("\ninvalid option... \n");
				 getch();
		}
	}
}

int menu()
{
	int a=0;
	clrscr();
	printf("Queue of 10 elements : \n\n");
	display();
	printf("\n\nMENU\n-------------\n");
	printf("1. add item\n2. delete item\n3. display items\n4. count items\n5. exit\nenter an option : ");
	scanf("%d",&a);
	return a;
}

void push()
{
	if(rear>=max-1 && queue[0]==NULL)
		rear=0;
	else if(rear<max)
		rear=rear+1;
	if(front==rear && queue[front]!=NULL)
	{
		printf("\nqueue overflow\n");
		return;
	}
	else
	{
		printf("\nenter element to add : ");
		scanf("%d",&element);
		queue[rear]=element;
	}
}
void pop()
{
	if(front==rear && queue[front]==NULL)
	{
		printf("\nqueue empty\n");
		return;
	}
	else
	{
		queue[front]=NULL;
		if(front==max-1)
			front=0;
		else
			front=front+1;
	}
}
void display()
{
	int temp=0;
	while(temp<max)
	{
		if(queue[temp]!=NULL)
			printf("%d ",queue[temp]);
		else
			printf("- ");
		temp++;
	}
}
int count()
{
	int temp=0,count=0;
	while(temp<max)
	{
		if(queue[temp]!=NULL)
			count++;
		temp++;
	}
	return count;
}

dqueue using array


#include< stdio.h>


int dqueue[10],front,rear,choice,max=10,element;


void add_front();
void add_rear();
void delete_front();
void delete_rear();
void display();
int count();



void main()
{
	front=-1;rear=0;

	while(1)
	{
		choice=menu();
		switch(choice)
		{
			case 1: add_front();getch();break;
			case 2: add_rear();getch();break;
			case 3: delete_front();getch();break;
			case 4:	delete_rear();getch();break;
			case 5: display();getch();break;
			case 6: printf("\nno of items in dqueue : %d\n",count());getch();break;
			case 7: exit();
			default : printf("\nInvalid option...\n");getch();
		}
	}
}

int menu()
{
	clrscr();
	printf("Queue elements : front=%d,rear=%d\n\n",front,rear);
	display();
	printf("\n\nMENU\n---------------------\n1. add item at front\n2. add item at back");
	printf("\n3. delete item from front\n4. delete item from rear\n5. display items\n6. count items\n7. exit");

	printf("\nenter your choice : ");
	scanf("%d",&choice);
	return choice;
}

void add_front()
{

	if(front<=-1)
	{	printf("\noperation failed.... can not add front\n");
		front=-1;
	}
	else
	{
		printf("\nenter element to add at front : ");
		scanf("%d",&element);
		dqueue[front+1]=element;  printf("\n%d\n",front);
		front--;
	}
}

void add_rear()
{

	if(rear>=max)
	{
		printf("\noperation failed....can not add at rear\n");
		rear=max;
	}
	else
	{
		printf("\nenter element to add at rear : ");
		scanf("%d",&element);
		dqueue[rear]=element;
		rear++;
	}
}

void delete_front()
{
	if(count()>0)
	{
		dqueue[front]=NULL;
		front++;
	}
	else
	{	printf("\noperation failed....can not del at front\n");
		front=-1;rear=0;
	}
}

void delete_rear()
{
	if(count()>0)
	{
		dqueue[rear]=NULL;
		rear--;
	}
	else
	{	printf("\noperation failed....can not del at rear\n");
		front=-1;
		rear=0;
	}
}

void display()
{
	int temp;
	temp=front;
	if(count()==0)
	{
		printf("\ndqueue empty\n");
		return;
	}
	else
	{
		while(temp<rear)
		{
			printf("%d ",dqueue[temp+1]);
			temp++;
		}
	}
}

int count()
{
	int temp;
	temp=rear-front-1;
	if(temp>0)
		return temp;
	else
		return 0;
}

queue using linkedlist

#include< stdio.h>
typedef struct queue
{
	int data;
	struct queue *next;
}list;

list *push(list *);
list *pop(list *);
void display(list *);
int menu(list *);
int count(list *);

void main()
{
	int choice;
	list *front,*rear;
	front=rear=NULL;

	while(1)
	{
		choice=menu(front);
		switch(choice)
		{
			case 1: rear=push(rear);
				break;
			case 2: front=pop(front);
				break;
			case 3: printf("\ndisplaying queue items... \n");
				display(front);
				getch();
				break;
			case 4: printf("\nno.of elements in queue : %d\n",count(front));
				getch();
				break;
			case 5:	exit();
			default: printf("\ninvalid option... \n");
				 getch();
		}
	}
}

int menu(list *front)
{
	int a=0;
	clrscr();
	printf("Queue Elements : \n\n");
	display(front);
	printf("\n\nMENU\n-------------\n");
	printf("1. add item\n2. delete item\n3. display items\n4. count items\n5. exit\nenter an option : ");
	scanf("%d",&a);
	return a;
}

list *push(list *rear)
{
	int element;
	list *temp;
	temp=(list *)malloc(sizeof(list));
	if(temp==NULL)
	{
		printf("\nInsufficient Memory...\n");
		getch();
		exit();
	}
	else
	{
		printf("\nenter element to add : ");
		scanf("%d",&element);

		temp->data=element;
		temp->next=NULL;
		rear->next=temp;
		rear=temp;
	}
	return (rear);
}

list *pop(list *front)
{
	list *temp;
	if(front->next==NULL)
	{
		printf("\nqueue is empty...\n");
		getch();
	}
	else
	{
		temp=front->next;
		free(front);
		front=temp;
	}
	return(front);
}

void display(list *front)
{
	list *temp;
	temp=front;
	if(temp->next==NULL)
		printf("\nqueue is empty...\n");
	else
	{
		while(temp->next!=NULL)
		{
			printf("%d ",temp->next->data);
			temp=temp->next;
		}
	}
}

int count(list *front)
{
	if(front->next==NULL)
		return 0;
	else
		return (1+count(front->next));
}

queue using array


#include< stdio.h>

int queue[10],front,rear,element,choice;

void push();
void pop();
void display();
int menu();
int count();

void main()
{
	front=rear=-1;
	while(1)
	{
		choice=menu();
		switch(choice)
		{
			case 1: push();
				getch();
				break;
			case 2: pop();
				getch();
				break;
			case 3: printf("\ndisplaying queue items... \n");
				display();
				getch();
				break;
			case 4: printf("\nno.of elements in queue : %d\n",count());
				getch();
				break;
			case 5:	exit();
			default: printf("\ninvalid option... \n");
				 getch();
		}
	}
}

int menu()
{
	int a=0;
	clrscr();
	printf("Queue of 10 elements : \n\n");
	display();
	printf("\n\nMENU\n-------------\n");
	printf("1. add item\n2. delete item\n3. display items\n4. count items\n5. exit\nenter an option : ");
	scanf("%d",&a);
	return a;
}

void push()
{
	if(rear<9)
	{
		rear++;
		if(rear>9)
		{
			printf("\nqueue overflow\n");
			return;
		}
		else
		{
			printf("\nenter element to add : ");
			scanf("%d",&element);
			queue[rear]=element;
		}
	}
	else
	{
		printf("\nqueue overflow\n");
		return;
	}
}
void pop()
{       if(front<rear)
	{
		front++;
		if(front>9)
		{
			printf("\nquque empty\n");
			front=rear=-1;
			return;
		}
		else
			printf("\nelement deleted from queue\n");
	}
	else
	{
		printf("\nquque empty\n");
		front=rear=-1;
		return;
	}
}
void display()
{
	int temp;
	temp=front;
	while(temp<rear)
	{
		printf("%d ",queue[temp+1]);
		temp++;
	}
}
int count()
{
	int temp,count=0;
	temp=front;
	while(temp<rear)
	{
		temp++;
		count++;
	}
	return count;
}


Sunday, April 1, 2012

singly linked list


#include< stdio.h>
typedef struct linked_list
{
 int data;
 struct linked_list *next;
}list;



void create(list *);
void traverse(list *);
void append(list *);
list *insert(list *);
list *delete_list(list *);
list *find(list *,int);
int count(list *);
int menu(list *);
int getdata(list *);



void main()
{
 list *head;
 int select;
 clrscr();

 head=(list *)malloc(sizeof(list));

 create(head);
 traverse(head);

 getch();
 while(1)
 {
  select=menu(head);
  switch(select)
  { case 1: head=insert(head);
    getch();
    break;
   case 2: head=delete_list(head);
    getch();
    break;
   case 3: printf("\n\nnumber of elements : %d",count(head));
    getch();
    break;
   case 4: append(head);
    break;
   case 5: traverse(head);
    getch();
    break;
   case 6: exit();
   default : printf("\ninvalid option... \n");getch();
  }
 }


}
int menu(list *start)
{
 int a=0;
 clrscr();
 printf("Your List : \n\n");
 traverse(start);
 printf("\n\nMENU\n-------------\n");
 printf("1. insert\n2. delete\n3. count\n4. append\n5. traverse\n6. exit\nenter an option : ");
 scanf("%d",&a);
 return a;
}

void create(list *start)
{
 printf("Enter element (Enter -1 to stop): ");
 scanf("%d",&start->data);
 if(start->data==-1)
  start->next=NULL;
 else
 {
  start->next=(list *)malloc(sizeof(list));
  create(start->next);
 }
}
int getdata(list *start)
{
 int element;
 while(1)
 {
  printf("\n\nenter element : ");
  scanf("%d",&element);
  if(element==-1)
  {
   printf("\noperation not allowed\n");
   getch();
   clrscr();
   printf("Your List : \n\n");
   traverse(start);
  }
  else
   return element;
 }
}

list *insert(list *start)
{
 int element,key;
 list *temp,*f;

 element=getdata(start);
 printf("enter the key element : ");
 scanf("%d",&key);

 if(start->data==key)
 {
  temp=(list *)malloc(sizeof(list));
  temp->data=element;
  temp->next=start;
  start=temp;
 }
 else
 {
  f=find(start,key);
  if(f==NULL)
   printf("\nkey not found\n");
  else
  {
   temp=(list *)malloc(sizeof(list));
   temp->data=element;
   temp->next=f->next;
   f->next=temp;
  }
 }
 return(start);
}

list *delete_list(list *start)
{
 int element;
 list *temp,*f;

 element=getdata(start);
 if(start->data==element)
 {
  temp=start->next;
  free(start);
  start=temp;
 }
 else
 {
  f=find(start,element);
  if(f==NULL)
   printf("\nelement not found\n");
  else
  {
   temp=f->next->next;
   free(f->next);
   f->next=temp;
  }
 }
 return (start);
}

list *find(list *start, int key)
{
 if(start->next->data==key)
  return (start);
 if(start->next->next==NULL)
  return NULL;
 else
  find(start->next,key);
}

int count(list *start)
{
 if(start->next!=NULL)
  return (1+count(start->next));
 else
  return 0;
}

void traverse(list *start)
{
 if(start->next!=NULL)
  printf("%3d ",start->data);
 else
  return;
 traverse(start->next);
}
void append(list *start)
{
 list *temp,*f;
 int element;
 element=getdata(start);

 f=find(start,-1);
 temp=(list *)malloc(sizeof(list));
 temp->data=element;
 temp->next=f->next;
 f->next=temp;
}