0% found this document useful (0 votes)
66 views

Data Structure Examples

The document contains code for implementing various data structures using C++ including stack, queue and linked list. It provides the class definitions and functions to perform operations like push, pop, add, delete etc on each of these data structures. The code shows how to create the classes with private and public members to manage the underlying arrays or nodes and provide the required interfaces to the user through member functions.

Uploaded by

korombish
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Data Structure Examples

The document contains code for implementing various data structures using C++ including stack, queue and linked list. It provides the class definitions and functions to perform operations like push, pop, add, delete etc on each of these data structures. The code shows how to create the classes with private and public members to manage the underlying arrays or nodes and provide the required interfaces to the user through member functions.

Uploaded by

korombish
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

STACK:-

#include <iostream>

using namespace std;

#define MAX 10        // MAXIMUM STACK CONTENT

class stack
{

  private:
    int arr[MAX];   // Contains all the Data
    int top;        //Contains location of Topmost Data pushed onto
Stack

  public:
         stack()         //Constructor
         {
            top=-1;      //Sets the Top Location to -1 indicating an
empty stack
         }

         void push(int a)  // Push ie. Add Value Function


         {
                top++;        // increment to by 1
                if(top<MAX)
                 {
                        arr[top]=a;  //If Stack is Vacant store Value
in Array
                 }
                 else
                 {
                        cout<<"STACK FULL!!"<<endl;
                        top--;
                 }
         }

        int pop()                  // Delete Item. Returns the


deleted item
        {
                if(top==-1)
                {
                        cout<<"STACK IS EMPTY!!!"<<endl;
                        return NULL;
                }
                else
                {
                        int data=arr[top];     //Set Topmost Value in
data
                        arr[top]=NULL;       //Set Original Location
to NULL
                        top--;               // Decrement top by 1
                        return data;         // Return deleted item
                }
         }
};

int main()
{
 stack a;
 a.push(3);
 cout<<"3 is Pushed\n";
 a.push(10);
 cout<<"10 is Pushed\n";
 a.push(1);
 cout<<"1 is Pushed\n\n";

 cout<<a.pop()<<" is Popped\n";
 cout<<a.pop()<<" is Popped\n";
 cout<<a.pop()<<" is Popped\n";
 return 0;
}
QUEUE:-

#include <iostream>

using namespace std;

#define MAX 5           // MAXIMUM CONTENTS IN QUEUE

class queue
{
 private:
        int t[MAX];
        int al;      // Addition End
        int dl;      // Deletion End

 public:
  queue()
  {
        dl=-1;
        al=-1;
  }

  void del()
  {
     int tmp;
     if(dl==-1)
     {
            cout<<"Queue is Empty";
     }
     else
     {
                for(int j=0;j<=al;j++)
                {
                        if((j+1)<=al)
                        {
                                tmp=t[j+1];
                                t[j]=tmp;
                        }
                        else
                        {
                                al--;

                        if(al==-1)
                                dl=-1;
                        else
                                dl=0;
                        }
                }
     }
  }

void add(int item)
{
        if(dl==-1 && al==-1)
        {
                dl++;
                al++;
        }
   else
   {
                al++;
                if(al==MAX)
        {
                        cout<<"Queue is Full\n";
                        al--;
                        return;
                }
        }
        t[al]=item;

  void display()
  {
        if(dl!=-1)
   {
        for(int iter=0 ; iter<=al ; iter++)
        cout<<t[iter]<<" ";
   }
   else
        cout<<"EMPTY";
  }

};

int main()
{
 queue a;
 int data[5]={32,23,45,99,24};

 cout<<"Queue before adding Elements: ";


 a.display();
 cout<<endl<<endl;

 for(int iter = 0 ; iter < 5 ; iter++)
 {
   a.add(data[iter]);
   cout<<"Addition Number : "<<(iter+1)<<" : ";
   a.display();
   cout<<endl;
 }
 cout<<endl;
 cout<<"Queue after adding Elements: ";
 a.display();
 cout<<endl<<endl;

 for(iter=0 ; iter < 5 ; iter++)
 {
   a.del();
   cout<<"Deletion Number : "<<(iter+1)<<" : ";
   a.display();
   cout<<endl;
 }
 return 0;
}
*********************************************************************
LINKEDLIST:-

#include <iostream>
using namespace std;

class linklist
{
        private:

         struct node
         {
            int data;
            node *link;
         }*p;

   public:

         linklist();
         void append( int num );
         void add_as_first( int num );
         void addafter( int c, int num );
         void del( int num );
         void display();
         int count();
         ~linklist();
};

linklist::linklist()
{
        p=NULL;
}

void linklist::append(int num)
{
   node *q,*t;

   if( p == NULL )
   {
      p = new node;
      p->data = num;
      p->link = NULL;
   }
   else
   {
      q = p;
      while( q->link != NULL )
        q = q->link;

      t = new node;
      t->data = num;
      t->link = NULL;
      q->link = t;
   }
}

void linklist::add_as_first(int num)
{
   node *q;

   q = new node;
   q->data = num;
   q->link = p;
   p = q;
}

void linklist::addafter( int c, int num)
{
   node *q,*t;
   int i;
   for(i=0,q=p;i<c;i++)
   {
      q = q->link;
      if( q == NULL )
      {
        cout<<"\nThere are less than "<<c<<" elements.";
        return;
      }
   }

   t = new node;
   t->data = num;
   t->link = q->link;
   q->link = t;
}

void linklist::del( int num )
{
   node *q,*r;
   q = p;
   if( q->data == num )
   {
      p = q->link;
      delete q;
      return;
   }

   r = q;
   while( q!=NULL )
   {
      if( q->data == num )
      {
         r->link = q->link;
         delete q;
         return;
      }

      r = q;
      q = q->link;
   }
   cout<<"\nElement "<<num<<" not Found.";
}
void linklist::display()
{
   node *q;
   cout<<endl;

   for( q = p ; q != NULL ; q = q->link )
        cout<<endl<<q->data;

int linklist::count()
{
   node *q;
   int c=0;
   for( q=p ; q != NULL ; q = q->link )
        c++;

   return c;
}

linklist::~linklist()
{
   node *q;
   if( p == NULL )
        return;

   while( p != NULL )
   {
      q = p->link;
      delete p;
      p = q;
   }
}

int main()
{
   linklist ll;
   cout<<"No. of elements = "<<ll.count();
   ll.append(12);
   ll.append(13);
   ll.append(23);
   ll.append(43);
   ll.append(44);
   ll.append(50);

   ll.add_as_first(2);
   ll.add_as_first(1);

   ll.addafter(3,333);
   ll.addafter(6,666);

   ll.display();
   cout<<"\nNo. of elements = "<<ll.count();

   ll.del(333);
   ll.del(12);
   ll.del(98);
   cout<<"\nNo. of elements = "<<ll.count();
   return 0;    
}

linklist();                       // CONSTRUCTOR


void append( int num );           // ADD AT END OF LIST
void add_as_first( int num );     // ADD TO BEGINNING OF LIST
void addafter( int c, int num );  // ADD DATA num AFTER POSTION c
void del( int num );              // DELETE DATA num
void display();                   // DISPLAY LINKED LIST
int count();                      // NUMBER OF ITEMS IN LIST
~linklist();                      // DESTRUCTOR

#include <stdio.h> /* for printf */


#include <stdlib.h> /* for malloc */

typedef struct node {


int data;
struct node *next; /* pointer to next element in list
*/
} LLIST;

LLIST *list_add(LLIST **p, int i);


void list_remove(LLIST **p);
LLIST **list_search(LLIST **n, int i);
void list_print(LLIST *n);

LLIST *list_add(LLIST **p, int i)


{
LLIST *n = (LLIST *) malloc(sizeof(LLIST));
if (n == NULL)
return NULL;

n->next = *p; /* the previous element (*p) now


becomes the "next" element */
*p = n; /* add new empty element to the front
(head) of the list */
n->data = i;

return *p;
}

void list_remove(LLIST **p) /* remove head */


{
if (*p != NULL)
{
LLIST *n = *p;
*p = (*p)->next;
free(n);
}
}

LLIST **list_search(LLIST **n, int i)


{
while (*n != NULL)
{
if ((*n)->data == i)
{
return n;
}
n = &(*n)->next;
}
return NULL;
}

void list_print(LLIST *n)


{
if (n == NULL)
{
printf("list is empty\n");
}
while (n != NULL)
{
printf("print %p %p %d\n", n, n->next, n-
>data);
n = n->next;
}
}

int main(void)
{
LLIST *n = NULL;

list_add(&n, 0); /* list: 0 */


list_add(&n, 1); /* list: 1 0 */
list_add(&n, 2); /* list: 2 1 0 */
list_add(&n, 3); /* list: 3 2 1 0 */
list_add(&n, 4); /* list: 4 3 2 1 0 */
list_print(n);
list_remove(&n); /* remove first (4)
*/
list_remove(&n->next); /* remove new second
(2) */
list_remove(list_search(&n, 1)); /* remove cell
containing 1 (first) */
list_remove(&n->next); /* remove second to
last node (0) */
list_remove(&n); /* remove last (3)
*/
list_print(n);

return 0;
}

STACK USING LINKEDLIST:-

#include <iostream>

using namespace std;
struct node
{
   int data;
   node *link;
};

class lstack
{
      private:
              node* top;

      public:
             lstack()
             {
                top=NULL;
             }
             
             void push(int n)
             {
                node *tmp;
                tmp=new node;
                if(tmp==NULL)
                   cout<<"\nSTACK FULL";
                   
                tmp->data=n;
                tmp->link=top;
                top=tmp;
             }

             int pop()
             {
                if(top==NULL)
                {
                        cout<<"\nSTACK EMPTY";
                        return NULL;
                }
                node *tmp;
                int n;
                tmp=top;
                n=tmp->data;
                top=top->link;
                delete tmp;
                return n;
             }

             ~lstack()
             {
                if(top==NULL)
                   return;

                node *tmp;
                while(top!=NULL)
                {
                   tmp=top;
                   top=top->link;
                   delete tmp;
                }
             }
};

int main()
{
        lstack s;
        s.push(11);
        s.push(101);
        s.push(99);
        s.push(78);
        cout<<"Item Popped = "<<s.pop()<<endl;
        cout<<"Item Popped = "<<s.pop()<<endl;
        cout<<"Item Popped = "<<s.pop()<<endl;
        return 0;
}

QUEUE USING LINKEDLIST:-


#include <iostream>

using namespace std;

struct node
{
   int data;
   node *link;
};

class lqueue
{
 private:
    node *front,*rear;

 public:
    lqueue()
    {
        front=NULL;
        rear=NULL;
    }

    void add(int n)
    {
        node *tmp;
        tmp=new node;
        if(tmp==NULL)
           cout<<"\nQUEUE FULL";

        tmp->data=n;
        tmp->link=NULL;
        if(front==NULL)
        {
                rear=front=tmp;
                return;
        }
            rear->link=tmp;
            rear=rear->link;
    }
    int del()
    {
        if(front==NULL)
        {
                cout<<"\nQUEUE EMPTY";
                return NULL;
        }
        node *tmp;
        int n;
        n=front->data;
        tmp=front;
        front=front->link;
        delete tmp;
        return n;
    }

    ~lqueue()
    {
        if(front==NULL)
           return;
        node *tmp;
        while(front!=NULL)
        {
                tmp=front;
                front=front->link;
                delete tmp;
        }
     }
};

int main()
{
        lqueue q;
        q.add(11);
        q.add(22);
        q.add(33);
        q.add(44);
        q.add(55);
        cout<<"\nItem Deleted = "<<q.del();
        cout<<"\nItem Deleted = "<<q.del();
        cout<<"\nItem Deleted = "<<q.del();
        return 0;
}

BINARY SEARCH :-
#include <iostream>

using namespace std;

#define YES 1
#define NO 0

class tree
{
        private:
                struct leaf
                {
                        int data;
                        leaf *l;
                        leaf *r;
                };
                struct leaf *p;

        public:
                tree();
                ~tree();
                void destruct(leaf *q);
                tree(tree& a);
                void findparent(int n,int &found,leaf* &parent);
             
void findfordel(int n,int &found,leaf *&parent,leaf* &x);
                void add(int n);
                void transverse();
                void in(leaf *q);
                void pre(leaf *q);
                void post(leaf *q);
                void del(int n);
};
                
tree::tree()
{
        p=NULL;
}

tree::~tree()
{
        destruct(p);
}
void tree::destruct(leaf *q)
{
        if(q!=NULL)
        {
                destruct(q->l);
                del(q->data);
                destruct(q->r);
        }
}
void tree::findparent(int n,int &found,leaf *&parent)
{
        leaf *q;
        found=NO;
        parent=NULL;

        if(p==NULL)
                return;

        q=p;
        while(q!=NULL)
        {
                if(q->data==n)
                {
                        found=YES;
                        return;
                }
                if(q->data>n)
                {
                        parent=q;
                        q=q->l;
                }
                else
                {
                        parent=q;
                        q=q->r;
                }
        }
}

void tree::add(int n)
{
        int found;
        leaf *t,*parent;
        findparent(n,found,parent);
        if(found==YES)
                cout<<"\nSuch a Node Exists";
        else
        {
                t=new leaf;
                t->data=n;
                t->l=NULL;
                t->r=NULL;

                if(parent==NULL)
                        p=t;
                else
                        parent->data > n ? parent->l=t : parent->r=t;
        }
}

void tree::transverse()
{
        int c;
        cout<<"\n1.InOrder\n2.Preorder\n3.Postorder\nChoice: ";
        cin>>c;
        switch©
        {
                case 1:
                        in(p);
                        break;

                case 2:
                        pre(p);
                        break;

                case 3:
                        post(p);
                        break;
        }
}

void tree::in(leaf *q)
{
        if(q!=NULL)
        {
                in(q->l);
                cout<<"\t"<<q->data<<endl;
                in(q->r);
        }
        
}

void tree::pre(leaf *q)
{
        if(q!=NULL)
        {
                cout<<"\t"<<q->data<<endl;
                pre(q->l);
                pre(q->r);
        }
        
}

void tree::post(leaf *q)
{
        if(q!=NULL)
        {
                post(q->l);
                post(q->r);
                cout<<"\t"<<q->data<<endl;
        }
        
}

void tree::findfordel(int n,int &found,leaf *&parent,leaf *&x)
{
        leaf *q;
        found=0;
        parent=NULL;
        if(p==NULL)
                return;

        q=p;
        while(q!=NULL)
        {
                if(q->data==n)
                {
                        found=1;
                        x=q;
                        return;
                }
                if(q->data>n)
                {
                        parent=q;
                        q=q->l;
                }
                else
                {
                        parent=q;
                        q=q->r;
                }
        }
}

void tree::del(int num)
{
        leaf *parent,*x,*xsucc;
        int found;

        // If EMPTY TREE


        if(p==NULL)
        {
                cout<<"\nTree is Empty";
                return;
        }
        parent=x=NULL;
        findfordel(num,found,parent,x);
        if(found==0)
        {
                cout<<"\nNode to be deleted NOT FOUND";
                return;
        }

        // If the node to be deleted has 2 leaves


        if(x->l != NULL && x->r != NULL)
        {
                parent=x;
                xsucc=x->r;

                while(xsucc->l != NULL)
                {
                        parent=xsucc;
                        xsucc=xsucc->l;
                }
                x->data=xsucc->data;
                x=xsucc;
        }

        // if the node to be deleted has no child


        if(x->l == NULL && x->r == NULL)
        {
                if(parent->r == x)
                        parent->r=NULL;
                else
                        parent->l=NULL;

                delete x;
                return;
        }

        // if node has only right leaf


        if(x->l == NULL && x->r != NULL )
        {
                if(parent->l == x)
                        parent->l=x->r;
                else
                        parent->r=x->r;

                delete x;
                return;
        }

        // if node to be deleted has only left child


        if(x->l != NULL && x->r == NULL)
        {
                if(parent->l == x)
                        parent->l=x->l;
                else
                        parent->r=x->l;

                delete x;
                return;
        }
}

int main()
{
        tree t;
        int data[]={32,16,34,1,87,13,7,18,14,19,23,24,41,5,53};
        for(int iter=0 ; iter < 15 ; i++)
                t.add(data[iter]);

        t.transverse();
        t.del(16);
        t.transverse();
        t.del(41);
        t.transverse();
        return 0;
}
Post fix :-
#include<iostream>

#include<conio.h>

using namespace std;

class Stack

private:

char s[100];

int depth;

public:

Stack()

depth=0;

void push(char x)

s[depth++]=x;

}
char pop()

if(depth<0)

depth=0;

if(depth>=1)

return s[--depth];

else

return 0;

bool IsEmpty()

return (depth==0);

char head()

if(depth>0)

return s[depth-1];

else

return 0;

};

void main()

Stack stack;

char *infix;
char expression[50];

cin>>expression;

infix=expression;

while(*infix!=0)

switch(*infix)

case '+':

if(stack.head() != 0)

while(!stack.IsEmpty())

cout<<stack.pop();

stack.push('+');

break;

case '-':

if(stack.head() != 0)

while(!stack.IsEmpty())

cout<<stack.pop();

stack.push('-');

break;

case '*':

if(stack.head()=='*'||
stack.head()=='/')

while(!stack.IsEmpty())

cout<<stack.pop();

stack.push('*');
break;

case '/':

if(stack.head()=='*'||
stack.head()=='/')

while(!stack.IsEmpty())

cout<<stack.pop();

stack.push('/');

break;

default:

cout<<(*infix);

*(infix++);

while(!stack.IsEmpty())

cout<<stack.pop();

getch();

return;

__________________

You might also like