#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;
}
}
}
Monday, April 2, 2012
cqueue using linkedlist
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;
}
stack using linkedlist
#include< stdio.h>
typedef struct stack
{
int data;
struct stack *next;
}list;
list *push(list *);
list *pop(list *);
void display(list *);
int count(list *);
int menu(list *);
void main()
{
int choice;
list *tos;
tos=(list *)malloc(sizeof(list));
tos->next=NULL;
while(1)
{
choice=menu(tos);
switch(choice)
{
case 1: tos=push(tos);
break;
case 2: tos=pop(tos);
getch();
break;
case 3: printf("\ndisplaying current stack : \n\n");
display(tos);
getch();
break;
case 4: exit();
default: printf("\ninvalid option... \n");
getch();
}
}
}
int menu(list *tos)
{
int a=0;
clrscr();
printf("no of elements in stack: %d\ncurrent stack : \n\n",count(tos));
display(tos);
printf("\n\nMENU\n-------------\n");
printf("1. push\n2. pop\n3. display\n4. exit\nenter an option : ");
scanf("%d",&a);
return a;
}
list *push(list *tos)
{
list *temp;
int element;
temp=(list *)malloc(sizeof(list));
if(temp==NULL)
{
printf("\nInsufficient memory...\n");
getch();
exit();
}
else
{
printf("\nenter element to push : ");
scanf("%d",&element);
temp->data=element;
temp->next=tos;
tos=temp;
}
return (tos);
}
list *pop(list *tos)
{
list *temp;
if(tos->next!=NULL)
{
temp=tos->next;
free(tos);
tos=temp;
printf("\nelement poped from stack\n");
}
else
printf("\nStack is Empty...\n");
return (tos);
}
void display(list *tos)
{
list *temp;
temp=tos;
if(temp->next==NULL)
printf("\nStack is Empty...\n");
else
{
while(temp->next!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
}
int count(list *tos)
{
if(tos->next==NULL)
return 0;
else
return (1+count(tos->next));
}
stack using array
#include< stdio.h>
int stack[10],top,element,choice;
void push();
void pop();
void display();
int menu();
int empty();
void main()
{
top=-1;
while(1)
{
choice=menu();
switch(choice)
{
case 1: push();
getch();
break;
case 2: pop();
getch();
break;
case 3: printf("\nno. of stack elements : %d\ndisplaying current stack :\n\n",top+1);
display();
getch();
break;
case 4: exit();
default: printf("\ninvalid option... \n");
getch();
}
}
}
int menu()
{
int a=0;
clrscr();
printf("Stack of 10 elements : \n\n");
display();
printf("\n\nMENU\n-------------\n");
printf("1. push\n2. pop\n3. display\n4. exit\nenter an option : ");
scanf("%d",&a);
return a;
}
void push()
{
if(empty()==0)
return;
else
{
top++;
printf("\nenter element to push : ");
scanf("%d",&element);
stack[top]=element;
}
}
void pop()
{
if(empty()==1)
return;
else
{
top--;
printf("\nelement poped from stack\n");
}
}
void display()
{
int temp;
temp=top;
if(empty()==1)
return;
else
{
while(temp>-1)
{
printf("%d ",stack[temp]);
temp--;
}
}
}
int empty()
{
if(top==-1)
{
printf("\nstack is empty\n");
return 1;
}
else if(top>9)
{
printf("\nstack is full\n");
return 0;
}
else
return 5;
}
Subscribe to:
Posts (Atom)