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

Recommended Answers

All 5 Replies

First, learn to use code tags when posting code to this board. It's not hard and it will preserve the indentation you hopefully have in the original code making it easier to read.

Glancing through your code it looks like you've managed to use a stack. a queue and a list in the same program. You could also have used the STL versions of these containers so you didn't have to write them out yourself, unless you're not allowed to use STL containers, though you're original post doesn't indicate that restriction.

Another version would be to base the Stack and Queue classes on a List rather than on an Array. That way it could expand/constrict easier.

If you have specific questions or problems, please post them.

what do you want from user as choice..for what purpose the user have to choose one???

This is code

// Stack using OOD

#include <iostream>

#include <stdlib.h>



using namespace std;



class Stack

{

private:

    enum { MAX=10,MIN=-1};  // MAXIMUM,MINIMUM STACK CONTENT

    int array[MAX]; // Contains all the Data

    int top;  //Contains location of Top most Data pushed onto Stack

public:

    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():

top(MIN)

{}



bool Stack::isEmpty()

{

    if(top == MIN)

        return true;

    else

        return false;

}



bool Stack::isFull()

{

    if(top == MAX -1)

        return true;

    else

        return false;

}



void Stack::push(int i)

{

    if(isFull())

    {

        cout<<"\nArray is Full";

        exit(1);

    }



    else

    {

        ++top;

        array[top]=i;

    }



}





int Stack::pop()

{

    int number;



    if(isEmpty())

    {

        cout<<"\nArray is Empty";

        exit(1);

    }



    else

    {

        number=array[top];

        top--;



    }



    return number;



}



class Queue

{

private:

    enum {MAX=10};

    int array[MAX];

    int back,front;



public:

    Queue();



    void enqueue(int e);

    int dequeue();

    bool isFull();

    bool isEmpty();

};





Queue::Queue():

back(0),front(0)

{}



void Queue::enqueue(int e)

{

    if(isFull())

    {

        cout<<"\nQueue is Full";

        exit(1);

    }



    back=(back+1)%MAX;

    array[back]=e;

}





int Queue::dequeue()

{

    if(isEmpty())

    {

        cout<<"\nQueue is Empty";

        exit(1);

    }



    front=(front+1)%MAX;

    return array[front];

}





bool Queue::isFull()

{

    if( (back+1)%MAX == front )

        return true;

    else

        return false;

}



bool Queue::isEmpty()

{

    if( back == front )

        return true;

    else

        return false;

}



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);

    }



}









int main()

{

    int cho;

    Stack s;

    Queue q;

    LinkedList *p=new LinkedList;



    cout<<"Enter the following\n1- for Stack\n2- for Queue\n3- for Linked List\n";

    cin>>cho;



    switch(cho){

    case 1:



        //////// what's code of stack ??????

        break;

    case 2:



        //// what's code of queue ?????

        break;

    case 3:



        // what's code of linked list ??

        break;

    default:

        cout<<"Wrong Choise Good Bye\n";

    }





    return 0;

}

before any other suggestion made to u...understand this one..
enclose your code with the code tags that is
[ code] your program [ /code]

That program use double lingked list, right?

commented: Over 2 years too late, the poster is probably long gone by now +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.