Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

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

Friday, October 11, 2013

I need single linked list implementation using templates in c++. it should have basic functions including swap(), append(), display(),delete().addatbeg() and addafter()?

// This program demonstrates the linked list template in c++.
#include <iostream.h>
#include "LinkedList.h"

void main(void)
{
LinkedList<int> list;
int a,b,position;

// Build the list
list.appendNode(2);
list.appendNode(4);
list.appendNode(6);
cout << "Here are the initial values:\n";
list.displayList();
cout << endl;
cout << "Now inserting the value 5.\n";
list.insertNode(5);
cout << "Here are the nodes now.\n";
list.displayList();
cout << endl;

cout << "Now deleting the last node.\n";
list.deleteNode(6);
cout << "Here are the nodes left.\n";
list.displayList();

cout<<"adding at begining";
list.addatbeg(18);
cout << "Here are the modified values:\n";
list.displayList();

cout<<"**********INSERTING ELEMENT AFTER SOME GIVEN NODE***********************";
cout<<endl;
cout<<"Enter the element to be inserted after some given node:";
cin>>a;

cout<<"Enter the position after which this element is inserted: ";
cin>>position;
list.addafter(a,position);


cout << "Here are the modified values:\n";
list.displayList();

cout<<"***************VISITING DATA OF SOME PARTICULAR NODE*********************";
cout<<endl;
cout<<"enter node no, whose data you want to visit";
cin>>a;
list.displayatnode(a);

cout<<"****************SWAPPING*************";
cout<<endl;
cout<<"enter nodes for which you want to swap data:";
cin>>a;
cout<<endl;
cin>>b;
list.swap(a,b);
cout<<"displaying after swapping"<<endl;
list.displayList();


}

//LinkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

template <class T>
class LinkedList
{
private:
// Declare a structure for the list
struct ListNode
{
T value;
struct ListNode *next;
};

ListNode *head;// List head pointer

public:

LinkedList(void)

// Constructor

{ head = NULL; }
~LinkedList(void); // Destructor
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList(void);
void addatbeg(T);
void addafter(T,T);
void displayatnode(int);
void swap(int,int);
};
// appendNode appends a node containing the
// value passed into num, to the end of the list.

template <class T>
void LinkedList<T>::appendNode(T num)
{
ListNode *newNode, *nodePtr;

// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;

else

// Otherwise, insert newNode at end

{
// Initialize nodePtr to head of list
nodePtr = head;

// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;

// Insert newNode as the last node
nodePtr->next = newNode;
}
}
// DisplayList shows the value
// stored in each node of the linked list
// pointed to by head.

template <class T>
void LinkedList<T>::displayList(void)
{
ListNode *nodePtr;

nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
template <class T>
void LinkedList<T>::addatbeg(T a)
{
ListNode *newNode;
newNode = new ListNode;
newNode->value=a;
newNode->next=head;
head=newNode;
}/*End of addatbeg( )*/

template <class T>
void LinkedList<T>::addafter(T data,T pos)
{
ListNode *tmp,*q;
tmp = new ListNode;
q = new ListNode;
q=head;
for(int i=0;i<pos-1;i++)
{
q=q->next;
if(q == NULL)
{
cout<<"There are less nodes";
return;
}
}/*End of for*/

tmp->next=q->next;
tmp->value=data;
q->next=tmp;

}/*End of addafter( )*/
template <class T>
void LinkedList<T>::swap(int node1,int node2)
{
ListNode *q,*p; int tempvalue;
//tmp = new ListNode;
q = new ListNode;
p = new ListNode;

q=head;
p=head;
for(int i=0;i<node1-1;i++)
{
q=q->next;

}/*End of for*/
for( i=0;i<node2-1;i++)
{
p=p->next;
}
tempvalue=p->value;
p->value=q->value;
q->value=tempvalue;
}/*End of addafter( )*/

template <class T>
void LinkedList<T>::displayatnode(int pos)
{
ListNode *k;
k= new ListNode;

if (head == NULL)
{
cout<<"List is empty";
return;
}
k=head;
for(int i=0;i<pos-1;i++)
{
k=k->next;
if(k == NULL)
{
cout<<"There are less nodes";
return;
}
}/*End of for*/

cout<<"value at this position is"<<":";
cout<< k->value;

}/*End of displayatnode( )*/

// The insertNode function inserts a node with
// num copied to its value member.
template <class T>
void LinkedList<T>::insertNode(T num)
{
ListNode *newNode, *nodePtr, *previousNode;

// Allocate a new node & store Num
newNode = new ListNode;
newNode->value = num;

// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else

// Otherwise, insert newNode at end

{
// Initialize nodePtr to head of list
nodePtr = head;

// Skip all nodes whose value member is less
// than num.
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Insert the node after the one pointed to
// by previousNode and before the one pointed to
// by nodePtr.
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
// The deleteNode function searches for a node
// with Num as its value. The node, if found, is
// deleted from the list and from memory.

template <class T>
void LinkedList<T>::deleteNode(T num)
{
ListNode *nodePtr, *previousNode;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;

// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after
// nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
// Destructor
// This function deletes every node in the list.

template <class T>
LinkedList<T>::~LinkedList(void)
{
ListNode *nodePtr, *nextNode;

nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
#endif

I need doubly linked list implementation in c++ using templates and linked list. please provide maximum functions?

Here is your running code...

#ifndef DOUBLY_H
#define DOUBLY_H
template <class T>
class doubly
{
private:
       class ListNode
       {
       public:
              T value;
              ListNode *next;
               ListNode *prev;
               ListNode()
               {
                     next=NULL;
                     prev=NULL;
               }
       };
       ListNode *head; // List head pointer
public:
       doubly(void)// Constructor
       {
              head = NULL;
       }
       void insertfirst(T);
       void insertlast(T);
       void Removefirst();
       void Removelast();
       void insertAfter(T,T);
       void insertBefore(T,T);
       void insertNode(T);
       void deleteNode(T);
       void displayList();
};
template <class T>
void doubly<T>::insertfirst(T num)
{
       ListNode *newnode ,*tptr;
       newnode= new ListNode;
       newnode->value=num;
       if(head==NULL)
       {
              head=newnode;
       }
       else
       {
              tptr=head;
              newnode->next=head;
              tptr->prev=newnode->next;
              head=newnode;
       }
}
template <class T>
void doubly<T>::insertlast(T num)
{
       ListNode *newnode,*tptr;
       newnode= new ListNode;
       newnode->value=num;
       if(head==NULL)
       {
              head=newnode;
       }
       else
       {
              tptr=head;
              while(tptr->next)
              {
                     tptr=tptr->next;
              }
              tptr->next=newnode;
              newnode->prev=tptr->next;
       }
}
template <class T>
void doubly<T>::Removefirst()
{
       ListNode *tptr,*tptr2;
       if(!head)
       {
              cout<<"Empty"<<endl;
       }
       else
       {
              tptr=head;
              tptr2=head;
              tptr2=tptr2->next;
              delete tptr;
              head=tptr2;
       }
}
template <class T>
void doubly<T>::Removelast()
{
       ListNode *tptr,*tptr2;
       if(!head)
       {
              cout<<"Empty"<<endl;
       }
       else
       {
              tptr=head;
              tptr2=head;
              while(tptr->next!=NULL)
              {
                     tptr2=tptr;
                     tptr=tptr->next;
              }
              delete tptr;
              tptr2->next=NULL;
       }
}
template <class T>
void doubly<T>::insertAfter(T key,T num)
{
       ListNode *newnode,*temp;
       T count=1;
       newnode=new ListNode;
       newnode->value=num;
       if(!head)
              cout<<"Empty "<<endl;
       else
       {
              temp=head;
              while(key!=count)
              {
                     temp=temp->next;
                     count++;
              }
              temp->next->prev=newnode;
              newnode->next=temp->next;
              newnode->prev=temp;
              temp->next=newnode;
       }     
}
template <class T>
void doubly<T>::insertBefore(T key,T num)
{
       ListNode *newnode,*temp,*temp2;
       T count=1;
       newnode=new ListNode;
       newnode->value=num;
       if(!head)
              cout<<"Empty "<<endl;
       else
       {
              temp=head;
              while(key!=count)
              {
                     temp2=temp;
                     temp=temp->next;
                     count++;
              }
              temp->prev=newnode;
              newnode->next=temp;
              temp2->next=newnode;
              newnode->prev=temp2;
       }

}
      
template <class T>
void doubly<T>::displayList()
{
       ListNode *nodeptr;
       if (head==NULL)
       {
              cout<<" Your list is empty "<<endl;
       }
       else
       {
              nodeptr=head;
              while (nodeptr!=NULL)
              {
                     cout<<nodeptr->value<<endl;
                     nodeptr=nodeptr->next;
              }
       }
}
template <class T>
void doubly<T>::deleteNode(T num)
{
       ListNode *temp,*temp2;
       if (head->value== num)
       {
              head=head->next;
       }
       else
       {
              temp=head;
              while(temp && temp->value!=num)
              {
                     temp2=temp;
                     temp=temp->next;
              }
              temp->next->prev=temp2->next;
              temp2->next=temp->next;
              delete temp;
       }
}
#endif

#include <iostream.h>
#include "doubly.h"
void main(void)
{
doubly<int> list;
cout<<"\t\tWelcome in doubly link list "<<endl;
cout<<"\nLets call insert function for inserting 2,4,6"<<endl;
list.insertfirst(2);
list.insertfirst(4);
list.insertfirst(6);
list.displayList();
cout<<"\nLets call insert function for inserting 8,10,12 in last"<<endl;
list.insertlast(8);
list.insertlast(10);
list.insertlast(12);
list.displayList();
cout<<"\nLets display forward list "<<endl;
list.displayList();
cout<<"\nDelete First \n";
list.Removefirst();
list.displayList();
cout<<"\nDelete last "<<endl;
list.Removelast();
list.displayList();
cout<<"\nDelete node number 2\n";
list.deleteNode(2);
list.displayList();
cout<<"\nInsert 14 after node 2\n";
list.insertAfter(2,14);
list.displayList();
cout<<"\nInsert 11 before node 3 "<<endl;
list.insertBefore(3,11);
list.displayList(); 

}

I need stack implementation via a linked list a complete running code.can someone help me please?

This is a complete running code in c++.
Enjoy

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

template <class T>
class LinkedList
{

private:
        // Declare a structure for the list
        struct ListNode
        {
                T value;
                    
               struct ListNode *next;
        };
ListNode *head; // List head pointer

public:
int top;
LinkedList(void)// Constructor
       {
                     top =-1;
                     head = NULL;

                    
       }     
              void push ();
              void pop ();
              bool empty()

              {
                     return head==NULL;

              }
              bool full ()
              {
                     return head!=NULL;
              }

       void displayList();
 };

template <class T >
void LinkedList <T>::push()
{
int num;

       ListNode *newnode ,*findptr;
if ( top != 3)
{
       newnode= new ListNode;
       cout<<" Enter a value of num ";
       cin>> num;
       newnode->value=num;
       newnode->next=NULL;
       if  ( head==NULL)
       {
             
              head=newnode;
      
              top++;
       }
       else
       {
              findptr=head;
             
              while ( findptr->next!=NULL)
              {
                     findptr=findptr->next;
              }
              findptr->next=newnode;
              newnode->next=NULL;
              top++;
       }
}
       else
       {
              cout<<" stack is full "<<endl;
       }
}


template <class T>

void LinkedList <T> ::pop()
{
       int num;


       if ( top!=-1)
       {
             
              cout<<" Enter a value for POP "<<endl;
              cin>> num;
       ListNode *movePtr,*pptr,*fptr;

       if (head== NULL )
             
       {
              cout<<" Your list is empty "<<endl;
       }

       else
       {
              if ( head->value== num)
              {
                     movePtr=head->next;
                     delete head;
                     head=movePtr;
              }
              else
              {
                     movePtr=head;
             

                     while (movePtr->value!= num && movePtr!=NULL )

                     {
                    
                           pptr=movePtr;
                           fptr=pptr;
                           movePtr=movePtr->next;
                           fptr=fptr->next->next;
                     }

                           movePtr->next=NULL;
                           movePtr->value=0;
                           delete movePtr;
                           pptr->next=fptr;
                          
              }
       }
       }
       else
       {
              cout<<" Stack is empty "<<endl;
       }
             
}
template <class T>
void LinkedList <T>::displayList()
{

              ListNode *moveptr;
      
       if ( head== NULL )
       {
              cout<<"  Your List is Empty "<<endl;
       }
       else
       {
              moveptr=head;

              while ( moveptr!=NULL)
              {
                     cout<<moveptr->value<<endl;
                     moveptr=moveptr->next;
              }

       }
}
#endif


#include <iostream>
using namespace std;
#include "code.h"
void main ()
{
       LinkedList<int> list;
       cout<<" 1 for push "<<endl;
       cout<<" 2 for pop "<<endl;
       cout<<" 3 for display "<<endl;
int x;

do {
       cout<<" ENTER A CHOICE  ";
       cin>> x;
       switch ( x)
      
       {
       case 1:

       list.push();
       break;
       case 2:
              list.pop( );
       break;
       case 3:
                     list.displayList();
       break;
       case 4 :
              break;

       }

}      while ( x !=4);


}