Monday, May 24, 2010

How can i write a program on C for double ended stack,circularly linked list?

If you have a program for singly linked list then I don't think it's very difficult to make it doubly linked list.





When you append a node to your linked list normally you keep it's next pointer as NULL. Just assign your "head" node pointer value to it and it becomes a circular list as last node now points to the first node!!!





similar is true about deleting the last node. just assign next of last node's to next of second last.





no change in insertions and deletions in between.





Now the catch can be - How to detect last node of a circle???


you can simply have a node count or every time you go to some node, just check whether it's next pointer is pointing to head node and it's done.








For Double ended stack.





It's just a stack having two heads, one at top and one at bottom. You can pop from any side and push from any.





Here you will have two top pointers than one. push and pop are same. one pointer's push is bit similar to others pop mechanism [only pointer changes]. e.g. one top pointer gets incremented when it pushes into array[taking array as it is easy to explain]. where as in others case it will decrement it's head as it pushes. [because it grows downwards].





Now what is empty and what is full?





Empty is when both of them cross each other. eg if top1 grows upward and top2 grows downward then ( top1 %26lt; top2 ) is a empty condition.





Full can be different for both stacks. if top1 reaches to upward limit then top1 is full and if top2 reaches downward limit then top2 is full.








I hope this will help you a bit. :)





enjoy.

How can i write a program on C for double ended stack,circularly linked list?
i will give you the program


#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#define max 5


class queue


{


private:


int qarr[max];


int front;


int rear;


public:


queue()


{


front=-1;


rear=-1;


}


void addqueue(int i)


{


if(((rear==max-1)%26amp;%26amp;(front==0))||(rear+...


{


cout%26lt;%26lt;"queue is full"%26lt;%26lt;endl;


return;


}


if(rear==max-1)


rear=0;


else


rear=rear+1;


qarr[rear]=i;


if(front==-1)


front=0;


}


int delqueue()


{


int d;


if(front==-1)


{


cout%26lt;%26lt;"queue is empty"%26lt;%26lt;endl;


return NULL;


}


else


{


d=qarr[front];


if(front==rear)


{


front=-1;


rear=-1;


}


else


{


if(front==max-1)


front=0;


else


front=front+1;


}


return d;


}


}


};


void main()


{


queue q1;


int ch,item;


cout%26lt;%26lt;"implementation of circular queue"%26lt;%26lt;endl;


do


{


clrscr();


cout%26lt;%26lt;"1.add queue"%26lt;%26lt;endl;


cout%26lt;%26lt;"2.delete queue"%26lt;%26lt;endl;


cout%26lt;%26lt;"quit:"%26lt;%26lt;endl;


cout%26lt;%26lt;"enter the choice(1-3)";


cin%26gt;%26gt;ch;


switch(ch)


{


case 1:


cout%26lt;%26lt;"enter the value...";


cin%26gt;%26gt;item;


q1.addqueue(item);


break;


case 2:


item=q1.delqueue();


cout%26lt;%26lt;"deleted the item:"%26lt;%26lt;item%26lt;%26lt;endl;


getch();


break;


case 3:


cout%26lt;%26lt;"bye....";


getch();


break;


}


}


while(ch!=3);


}








hope this helped you.


good luck.
Reply:AJUDA AÍ.


No comments:

Post a Comment