anyone can do case statment for stack , queue and linked in one program plz
ex:
void main ()
do
{
cout << "1 - stack";
cout << "2 -queue";
cout << "3 - linked list ";
cout << "4 - stack with linked list";
cout << "5 -queue ....... ";
swatch (choice)
case 1 : what's a code of stack here ?
case 2 : what's a code of queue here ?
case 3 : what's a code linked list here ?
code stack :
class Stack
{
private:
enum { MAX=10,MIN=-1};// MAXIMUM,MINIMUM STACK CONTENT
int *pArray; // pointer to array.
int top; //Contains location of Top most Data pushed onto Stack
int size; // contain the number of item(integer) you want
public:
Stack(int n=MAX);
~Stack();
void push(int); // push number to array
int pop(); // pop it from array
bool isEmpty(); // check if array is Empty!
bool isFull(); // check if it full
};
Stack::Stack(int n):
top(MIN),size(n)
{
pArray=new int[n];
}
Stack::~Stack()
{
delete pArray;
pArray=0;
}
bool Stack::isEmpty()
{
return (top==MIN?ture:false);
}
bool Stack::isFull()
{
return (top==size -1 ?ture:false);
}
void Stack::push(int i)
{
if(isFull())
{
cout<<"\nArray is Full";
exit(1);
}
else
{
++top;
*(pArray+top)=i;
// or use this : pArray[top]=i;
}
}
int Stack::pop()
{
int number;
if(isEmpty())
{
cout<<"\nArray is Empty";
exit(1);
}
else
{
number=pArray[top];
//or use this: number=*(pArray+top);
top--;
}
return number;
}
code queue:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Queue
{
private:
enum {MAX=10};
int *pArray;
int back,front;
int size;
public:
Queue(int s=MAX);
~Queue();
void enqueue(int e);
int dequeue();
bool isFull();
bool isEmpty();
};
Queue::Queue(int s):
size(s),back(0),front(0)
{
pArray=new int[s];
}
Queue::~Queue()
{
delete [] pArray;
pArray=0;
}
void Queue::enqueue(int e)
{
if(isFull())
{
cout<<"\nQueue is Full";
exit(1);
}
back=(back+1)%size;
pArray[back]=e;
}
int Queue::dequeue()
{
if(isEmpty())
{
cout<<"\nQueue is Empty";
exit(1);
}
front=(front+1)%size;
return pArray[front];
}
bool Queue::isFull()
{
if( (back+1)%size == front )
return true;
else
return false;
}
bool Queue::isEmpty()
{
if( back == front )
return true;
else
return false;
}
int main()
{
Queue q(10);
cout<<"\nenqueue(2)";
q.enqueue(2);
cout<<"\nenqueue(4)";
q.enqueue(4);
cout<<"\nenqueue(6)";
q.enqueue(6);
cout<<"\nDequeue elements\n";
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
return 0;
}
licked list :
#include <iostream>
using namespace std;
/////// Node Class //////
class Node
{
private:
int data;
Node *pNext;
Node *pPrevious;
public:
Node(int d):data(d),pNext(NULL),pPrevious(NULL){}
int getData()const{return data;}
void setData(int d){data=d;}
Node * getNext()const{return pNext;}
void setNext(Node *p){pNext=p;}
Node * getPrevious()const {return pPrevious;}
void setPrevious(Node *p){pPrevious=p;}
};
/////// LinkedList class ///////
class LinkedList
{
protected:
Node *pHead; //pointer to the first node
Node *pTail; //pointer to the last node
int nodeCounter; // count the number of nodes
Node * makeNode(int data); // make & return new node
void remove(Node *tmp); // remove node from linked list
public:
LinkedList();
~LinkedList();
// operations
void addFirst(int data);
void addLast(int data);
void addBefore(int index,int data);
void removeFirst();
void removeLast();
void displayInOrder()const;
void displayInReverseOrder()const;
void destroyList();
int getSize()const{return nodeCounter;}
};
LinkedList::LinkedList():
pHead(NULL),pTail(NULL),nodeCounter(0)
{}
LinkedList::~LinkedList()
{
destroyList();
}
void LinkedList::displayInOrder()const
{
if( !pHead )
{
cout<<"\nNULL";
return ;
}
for(Node *cur=pHead;cur!=NULL;cur=cur->getNext())
cout<<cur->getData()<<" -> ";
cout<<"NULL"<<endl;
}
void LinkedList::displayInReverseOrder()const
{
if( !pHead )
{
cout<<"\nNULL";
return ;
}
for(Node *cur=pTail;cur!=NULL;cur=cur->getPrevious())
cout<<cur->getData()<<" -> ";
cout<<"NULL"<<endl;
}
Node * LinkedList::makeNode(int data)
{
nodeCounter++;
return new Node(data);
}
void LinkedList::addFirst(int data)
{
// make new node
Node *pNew=makeNode(data);
// make the new node as the first node in the linked list
if( !pHead ) // if linkedlist is empty
pHead=pTail=pNew;
else // if there are nodes
{
pNew->setNext(pHead);
pHead->setPrevious(pNew);
pHead=pNew;
}
}
void LinkedList::addLast(int data)
{
// make new node
Node *pNew=makeNode(data);
// make the new node as the last node in the linked list
//if there is no node in the linked list , add node.
//otherwise move the new node to the last.
if( !pHead )
pHead=pTail=pNew;
else
{
pTail->setNext(pNew);
pNew->setPrevious(pTail);
pTail=pNew;
}
}
void LinkedList::addBefore(int index,int data)
{
// check the index to see if it's legal
if( index <1 || index > getSize() )
{
cerr<<"\nError: index is too hight/low ";
return;
}
if(index == 1)
{
addFirst(data);
return ;
}
// make new Node
Node *pNew=makeNode(data);
Node *pCur=pHead;
for(int i=1;i<index -1;i++,pCur=pCur->getNext());
pNew->setNext(pCur->getNext());
pCur->setNext(pNew);
}
void LinkedList::removeFirst()
{
if( !pHead )
{
cerr<<"\nThere is no any node to remove!!";
return ;
}
Node *pTmp=pHead;
pHead=pHead->getNext();
if(pHead)
pHead->setPrevious(NULL);
else
pHead=pTail=NULL;
remove(pTmp);
}
void LinkedList::remove(Node *tmp)
{
delete tmp;
nodeCounter--;
};
void LinkedList::removeLast()
{
if( !pHead )
{
cerr<<"\nThere is no any node to remove!!";
return ;
}
Node *pTmp=pTail;
pTail=pTail->getPrevious();
if(pTail)
pTail->setNext(NULL);
else
pTail=pHead=NULL;
remove(pTmp);
}
void LinkedList::destroyList()
{
Node *pTmp=pHead;
Node *pCur;
while( pTmp != NULL)
{
pCur=pTmp;
pTmp=pTmp->getNext();
remove(pCur);
}
}
/////// main //////////
int main()
{
LinkedList *p=new LinkedList;
for(int i=0;i<5;i++)
p->addLast(2*i+1);
cout<<"\nPrint linked list in order\n";
p->displayInOrder();
cout<<"\nPrint linked list in reverse order\n";
p->displayInReverseOrder();
cout<<"\nadd 20 befor the second node. \n";
p->addBefore(2,20);
cout<<"\nPrint linked list in order\n";
p->displayInOrder();
return 0;
}
// EOF