Tuesday, November 5, 2013

I need to program stack using doubly linked list in proper separated files, i.e header and cpp file. can anyone here to help me please? i need running code

Here is your full code.

//this is dllist.h file

#ifndef DLLIST_H
#define DLLIST_H
template <class T>
class dLinkedList
{
private:

struct Node
{
double value;
Node *next;
Node *prev;
};

Node *head;
public:

dLinkedList() //constructor
{
head = NULL;
}
//~DoublyLinkedList(); //destructor (deletes links)
void insertFirst(T); //insert at front of list
void insertLast(T); //insert at end of list
void removeFirst(); //remove first link
void removeLast(); //remove last link
void displayList(); // display List items
};
template <class T>
void dLinkedList<T>::insertFirst(T d)
{
Node *newNode;

newNode = new Node;
newNode->value = d;
newNode->next = NULL;
newNode->prev = NULL;

if(head == NULL)
{
head = newNode;
}
else
{
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}
template <class T>
void dLinkedList<T>::displayList()
{
Node *nodePtr;

if(head == NULL)
cout<<"List is Empty \n";
else
{
nodePtr = head;
cout<<"\nList is : ";
while(nodePtr)
{
cout<<nodePtr->value<<" ";
nodePtr = nodePtr->next;
}
}
}
template <class T>
void dLinkedList<T>::insertLast(T d)
{
Node *newNode, *nodePtr;

newNode = new Node;
newNode->value = d;
newNode->next = NULL;
newNode->prev = NULL;

if(head == NULL)
{
head = newNode;
}
else
{
nodePtr = head;
while (nodePtr -> next)
{
nodePtr = nodePtr -> next;
}
nodePtr -> next = newNode;
newNode -> prev = nodePtr -> next;
newNode -> next = NULL;
cout << endl;
}
}
template <class T>
void dLinkedList<T> :: removeFirst ()
{
Node *node, *nodePtr;
if (!head)
{
cout << "List is Empty" << endl;
return;
}
else
{
node = head;
nodePtr = node -> next;
delete node;
head = nodePtr;
nodePtr ->prev = NULL;
}
}
template <class T>
void dLinkedList<T>:: removeLast()
{
Node *node, *nodePtr;
if (!head)
{
cout <<"List Is Empty" << endl;
return;
}
else
{
node = head;
nodePtr = head;
while (node -> next != NULL)
{
nodePtr = node;
node = node -> next;
}
delete node;
nodePtr -> next = NULL;
}
}
#endif

//here is stack.h file

#ifndef STACK_H
#define STACK_H
#include"dllist.h"
const int SIZE=4;
template <class T>
class stack
{
private:

struct Node
{
double data;
Node *next;
};

Node *Top;
int length;

public:
dLinkedList<int> dll;

stack()
{
Top = NULL;
length = 0;
}

void push(T);
void display();
void pop();
bool isEmpty()
{
if(Top == NULL && length == 0)
return 1;
else
return 0;
}
bool isFull()
{
if(length == SIZE)
return 1;
else
return 0;
}

};
template <class T>
void stack<T>::push(T d)
{
if(isFull())
{
cout<<"Stack is Full";
}
else
{
dll.insertLast(d);
length++;

}
}
template <class T>
void stack<T>::display()
{
cout<<endl;
if(isEmpty())
cout<<"Stack is empty";
else
{
dll.displayList();

    }

}
template <class T>
void stack<T>::pop()
{
dll.removeFirst();
length--;
}

#endif

//this is main program demonstrating its functionality

#include<iostream.h>
#include"stack.h"
#include"dllist.h"
void main()
{
stack<int> st;
int choice;
while(choice!=4)
{
cout<<"\n1 Push\n2 Pop\n3 Display\n4 Exit\n Enter your choice : ";
cin>>choice;

switch(choice)
{
case 1:
{
if(st.isFull())
{
cout<<"\nStack Is Full\n";
break;
}
else
{
int value;
cout<<"Enter the new value to be pushed on the stack : ";
cin>>value;
st.push(value);
}
}
break;
case 2:
{
if(st.isEmpty())
{
cout<<"\nStack Is Empty !!\n";
break;
}
else
st.pop();
}
break;
case 3:
{
st.display();
}
break;
case 4:
break;
default:
cout<<"\nWrong Entery Enter Again !!\n";
}
}

}
this is running code. i hope you like it

2 comments:

  1. I am truly thankful to the holder of this web page who has shared this great post
    at at this time.

    my web-site - Star Wars Rebel Recon Hack

    ReplyDelete
  2. Thanks for a marvelous posting! I truly enjoyed reading it, you may
    be a great author.I will ensure that I bookmark your blog and may
    come back later on. I want to encourage that you continue your
    great job, have a nice morning!

    my web page ... minecraft games

    ReplyDelete