Thursday, January 15, 2015

Implementation of Stack by linklist

#include <iostream>
#include<stdio.h>
#include <conio.h>
using namespace std;
struct node
{
    int info;
    struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void display();
void destroy();
void create();
int count = 0;
int main()
{
    int no, ch, e;
    printf("\n 1 - Push");
    printf("\n 2 - Pop");
    printf("\n 3 - Top");
    printf("\n 4 - Empty");
    printf("\n 5 - Exit");
    printf("\n 6 - Dipslay");
    printf("\n 7 - Stack Count");
    printf("\n 8 - Destroy stack");
    create();
    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("Enter data : ");
            scanf("%d", &no);
            push(no);
            break;
        case 2:
            pop();
            break;
        case 3:
            if (top == NULL)
                printf("No elements in stack");
            else
            {
                e = topelement();
                printf("\n Top element : %d", e);
            }
           
            break;
        case 4:
            exit(0);
        case 5:
            display();
            break;
        default :
            printf(" Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}

/* Create empty stack */
void create()
{
    top = NULL;
}

/* Count stack elements */


/* Push data into stack */
void push(int data)
{
    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        top->info = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->ptr = top;
        temp->info = data;
        top = temp;
    }

}

/* Display stack elements */
void display()
{
    top1 = top;

    if (top1 == NULL)
    {
        printf("Stack is empty");
        return;
    }

    while (top1 != NULL)
    {
        printf("%d ", top1->info);
        top1 = top1->ptr;
    }
 }

/* Pop Operation on stack */
void pop()
{
    top1 = top;

    if (top1 == NULL)
    {
        printf("\n Error : Trying to pop from empty stack");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %d", top->info);
    free(top);
    top = top1;

}

/* Return top element */
int topelement()
{
    return(top->info);
}

How to implement Binary Search Tree and show output as Inorder, Preorder and Post Order?

#include<conio.h>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
struct node
{
struct node *lchild,*rchild;
int info;
}*root;
void find(int,struct node **,struct node **);
void insert(int);
void del(int);
void case_a(struct node*,struct node*);
void case_b(struct node*,struct node*);
void case_c(struct node*,struct node*);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
main()
{
int choice,num;
root=NULL;
while(1)
{
cout<<"\n";
cout<<"1.INSERT"<<endl;
cout<<"2. DELETE"<<endl;
cout<<"3. INORDER TRAVERSAL"<<endl;
cout<<"4. PREORDER TRAVERSAL"<<endl;
cout<<"5. POSTORDER TRAVERSAL"<<endl;
cout<<"6. QUIT"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the number to be inserted"<<endl;
cin>>num;
insert(num);
break;
case 2:
cout<<"Enter the number to be deleted"<<endl;
cin>>num;
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(1);
default:
cout<<"WRONG CHOICE"<<endl;
}
}
}
void find(int item,struct node**par,struct node**loc)
{
struct node *ptr,*ptrsave;
if(root==NULL)
{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info)
{
*loc=root;
*par=NULL;
return;
}
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{
if(item==ptr->info)
{
*loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=NULL;
*par=ptrsave;
}
void insert(int item)
{
struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
cout<<"Item Is Already Present"<<endl;
return;
}
tmp=new(struct node);
tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}
void del(int item)
{
struct node *parent,*location;
if(root==NULL)
{
cout<<"TREE EMPTY"<<endl;
return;
}
find(item,&parent,&location);
if(location==NULL)
{
cout<<"item not presented in the tree"<<endl;
return;
}
if(location->lchild==NULL&&location->rchild==NULL)
case_a(parent,location);
if(location->lchild!=NULL&&location->rchild==NULL)
/*case_b(parent,location);
if(location->lchild==NULL&&location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL&&location->rchild!=NULL)
case_c(parent,location);*/
free(location);
}
void case_a(struct node *par,struct node *loc)
{
if(par==NULL)
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
}
void case_b(struct node *par,struct node *loc)
{
struct node *child;
if(loc->lchild!=NULL)
child=loc->lchild;
else
child=loc->rchild;
if(par==NULL)
root=child;
else
if(loc==par->lchild)
par->lchild=child;
else
par->rchild=child;
}
void case_c(struct node *par,struct node *loc)
{
struct node *ptr,*ptrsave,*suc,*parsuc;
ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{
ptrsave=ptr;
ptr=ptr->lchild;
}
suc=ptr;
parsuc=ptrsave;
if(suc->lchild==NULL&&suc->rchild==NULL)
case_a(parsuc,suc);
else
case_b(parsuc,suc);
if(par==NULL)
root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}
void preorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
cout<<ptr->info<<"\t";
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}
void inorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
cout<<ptr->info<<"\t";
inorder(ptr->rchild);
}
}
void postorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
cout<<ptr->info<<"\t";
}
}



How to implement Stack by Array in C++?

#include<iostream>
#include<conio.h>
#include<stdio.h>
//#include<stack>
using namespace std;


#define max 5
int Stack_Array[max];
int top=-1;
void push();
void pop();
void display();

main()
{
int choice;
while(1)
{
cout<<"1: Push"<<endl;
cout<<"2: Pop"<<endl;
cout<<"3: Display"<<endl;
cout<<"4:Quit"<<endl;
cin>>choice;

switch(choice)
{
case 1:
push();
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
return 0;;

default:
cout<<"Wrong Choice"<<endl;

}  //end of switch
}  //end of while
}   //end of main

void push()
{
int pushed_item;
if(top==(max-1))
    cout<<"Stack_Array Overflow"<<endl;
else
{
    cout<<"Enter the item to be pushed in Stack_Array:"<<endl;
    cin>>pushed_item;
    top=top+1;
    Stack_Array[top]=pushed_item;

}
}


void pop()
{
if(top==-1)
    cout<<"Stack_Array Underflow"<<endl;
else
{
    cout<<"Poped element is:"<<Stack_Array[top]<<endl;
    top=top-1;
}
}


void display()
{
int i;
if(top==-1)
    cout<<"Stack_Array Empty"<<endl;
else
{
    cout<<"Stack_Array element:"<<endl;
    for(i=top;i>=0;i--)
    cout<<Stack_Array[i]<<endl;
}
}

How to implement Circular Queue By Linklist?

//Circular Queue Through Linklists
#include<iostream>
#define SIZE 100
#include<conio.h>
using namespace std;
struct circularque
{
  int data;
  struct circularque *next;
}*f=NULL,*r=NULL,*n,*temp,*temp1;

void insert();
void delet();
void display();

int main()
{
  int ch;
  do
  {
     cout<<"\n\n\tMain Menu";
     cout<<"\n****************************";
     cout<<"\n1. Insert\n2. Delete\n3. Display\n4. Exit\n\nEnter Your Choice: ";
     cin>>ch;
     switch(ch)
     {
    case 1:
      insert();
      display();
      break;
    case 2:
      delet();
      break;
    case 3:
    display();
      break;
      case 4:
    default:
      cout<<"\n\nWrong Choice!!! Try Again.";
     }
  }while(ch!=4);
  return 0;
}

void insert()
{
  n=new circularque[sizeof(circularque)];
  cout<<"\nEnter the Element: ";
  cin>>n->data;
  if(f==NULL)
  {
      f=n;
  }
  else
  {
      r->next=n;
  }
  r=n;
  r->next=f;
}
void delet()
{
  int x;
  temp=f;
  if(f==NULL)
  {
      cout<<"\nCircular Queue Empty!!!";
  }
  else
  {
     if(f==r)
     {
       x=f->data;
       delete(temp);
       f=NULL;
       r=NULL;
     }
     else
     {
    x=temp->data;
    f=f->next;
    r->next=f;
    delete(temp);
     }
     cout<<"\nElement "<<x<<" is Deleted";
     display();
  }
}
void display()
{
  temp=f;
  temp1=NULL;
  if(f==NULL)
  {
    cout<<"\n\nCircular Queue Empty!!!";
  }
  else
  {
    cout<<"\n\nCircular Queue Elements are:\n\n";
    while(temp!=temp1)
    {
       cout<<temp->data<<"  ";
       temp=temp->next;
       temp1=f;
    }
  }
}

How to implemet Linear Queue By Linklist?

#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
                int data;
                struct node *next;
}*front=NULL,*rear,*temp;
void ins()
{
                temp=new node;
                cout<<"Enter data:";
                cin>>temp->data;
                temp->next=NULL;       

                if(front==NULL)
                                front=rear=temp;
                else
                {
                                rear->next=temp;
                                rear=temp;
                }
                cout<<"Node has been inserted\n";     
}
void del()
{
                if(front==NULL)
                                cout<<"Queue is empty\n";
                else
                {
                                temp=front;
                                front=front->next;
                                cout<<"Deleted node is "<<temp->data<<"\n";
                                delete(temp);
                }
}
void dis()
{
                if(front==NULL)
                                cout<<"Queue is empty\n";
                else
                {
                                temp=front;
                                while(temp->next!=NULL)
                                {
                                                cout<<temp->data<<"->";
                                                temp=temp->next;
                                }

                                cout<<temp->data;
                }
}
main()
{
                int ch;
                while(1)
                {
                                cout<<"\n*** Menu ***"<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
                                cout<<"\n\nEnter your choice(1-4):";
                                cin>>ch;
                                cout<<"\n";

                                switch(ch)
                                {
                                                case 1:  ins();
                                                                break;
                                                case 2: del();
                                                                break;
                                                case 3: dis();
                                                                break;
                                                case 4: exit(0);
                                                                break;
                                                default: cout<<"Wrong Choice!!!";
                                }
                }

                return 0;
}

How to create,insert,delete and reverse Linear Doubly linklist in C++?

//Lab 7 Solution
#include<iostream>
#include<conio.h>
#include<malloc.h>
#include<process.h>
using namespace std;
void create_list();
void display();
void addatbeg( );
void addafter(int);
void del(int);
void rev();
struct node{
       int data;
       struct node *next;
       struct node *prev;
}* start;

int main(){

int choice,n,position,i;
int a;
int b;
start=NULL;
while(1)
{
cout<<"1. Create List"<<endl;
cout<<"2. Display List"<<endl;
cout<<"3. Add at Begining"<<endl;
cout<<"4. Add in middle"<<endl;
cout<<"5. Deletion"<<endl;
cout<<"6. Reverse"<<endl;
cout<<"7. Quit Program"<<endl;


cin>>choice;
switch(choice)
{
case 1:
cout<<"How many Nodes you Want? "<<endl;
cin>>n;
for (i=1;i<=n;i++)
{

create_list();
}
break;
case 2:
display();
break;
case 3:
    

addatbeg();
break;
case 7:
exit(0);
case 4:
    

cout<<"Enter position";
cin>>position;
addafter(position);
break;
case 5:
     int m;
     cout<<"enter Element to be deleted"<<endl;
     cin>>m;
     del(m);
     break;
     case 6:
          rev();
          display();
          break;
     default:
cout<<"Wrong Choice"<<endl;
}
}
getch();
return 0;
}
void create_list()
{
struct node *q,*tmp;
tmp=(node*)malloc(sizeof(struct node));
cout<<"Enter data"<<endl;
cin>>tmp->data;
tmp->next=NULL;
tmp->prev=NULL;
if(start==NULL)
{
tmp->prev=NULL;
start=tmp;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=tmp;
tmp->prev=q;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
cout<<"Empty List"<<endl;
return;
}
else
{
q=start;
while(q!=NULL)
{
cout<<q->data<<"\t";
q=q->next;
}
}
cout<<"\n";
}
void addatbeg()
{
struct node *tmp;
tmp=(node *)malloc(sizeof(struct node));

cout<<"enter data"<<endl;
cin>>tmp->data;
tmp->prev=NULL;
tmp->next=start;
start->prev=tmp;
start=tmp;
}
void addafter(int pos)
{
struct node *tmp, *q;
tmp=(node *)malloc(sizeof(struct node));
int i;
q=start;
for(i=1;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
cout<<"There are less elements"<<pos;
return;
}
}
tmp=(node *)malloc(sizeof(struct node));
cout<<"Enter data"<<endl;
cin>>tmp->data;
q->prev=tmp;
tmp->next=q->next;
tmp->prev=q;
q->next=tmp;
}

void del(int data)
{
     struct node *q,*tmp;
     if(start->data==data)
     {
                          tmp=start;
                          start=start->next;
                          start->prev=NULL;
                          free(tmp);
                          return;
                          }
                          q=start;
                          while(q->next!=NULL)
                          {
                                                    if(q->data==data)
                                                    {
                                                                            tmp=q->next;
                                                    q->next=tmp->next;
                                                    tmp->next->prev=q;
                                                    free(tmp);
                                                    return;
                                                    }
                                                    q=q->next;
                                                    }
                                                    if(q->data==data)
                                                    {
                                                                           tmp=q->next;
                                                                          
                                                                           q->next=NULL;
                                                                           free(tmp);
                                                                            return;
                                                                           }
}
void rev()
{
     struct node *p1,*p2;
     p1=start;
     p2=p1->next;
     p1->next=NULL;
     p1->prev=p2;
     while(p2!=NULL)
     {
                    p2->prev=p2->next;
                    p2->next=p1;
                    p1=p2;
                    p2=p2->prev;
                    }
                    start=p1;
}
              
                                                                               

How to create,insert,delete and reverse Doubly Circular linklist in C++?

#include<iostream>
#include<conio.h>
using namespace std;
 struct circular
{
    int i;
    struct circular *front;
    struct circular *back;
};
struct circular *temp;
struct circular *head;
struct circular *p;
struct circular *mid;
struct circular *move;

int cnt=0;


void create(void);
void insert(void);
void display(void);
void del(void);



//Creating a node
void create()
{

   
    head=(struct circular *)malloc(sizeof(struct circular));
    head->back=head;
    head->front=head;

    cout<<"ENETER THE DATA\n";
    cin>>head->i;
    temp=head;

    temp->back=(struct circular *)malloc(sizeof(struct circular));
    temp=temp->back;
    temp->back=head;
    head->front=temp;
cout<<"ENETER THE DATA\n";
    cin>>temp->i;


   
}

//Displaying the list
void display()
{
    p=head;
    cout<<p->i;
    p=p->back;
    while(p!=head)
    {
        cout<<p->i;
        p=p->back;
    }
    cout<<"\n";

}

//Inserting a new node
void insert()
{
    int add,t;

    cout<<"\n\t ENTER ANY NUMBER BETWEEN 1 AND %d\n"<<cnt;
    cin>>add;
    p=head;
    t=1;
    while(t<add)
    {
        p=p->back;
        t++;
    }
    mid=(struct circular *)malloc(sizeof(struct circular));
    cout<<"\n\n\nENETER THE DATA\n";
    cin>>mid->i;

    mid->back=p->back;
    p->back=mid;
    p->back->front=mid;
    mid->front=p;

}

//Deleting a node
void del(void)
{
    int add,t;

    cout<<"\n\n\t ENTER ANY NUMBER BETWEEN 1 AND %d\n"<<cnt;
    cin>>add;
    p=head;
    t=1;
    while(t<add-1)
    {
        p=p->back;
        t++;
    }
  
    mid=p->back;
    p->back=mid->back;
    mid->back->front=p;
    free(mid);
}


//Calling in main()
int main()
{
    int ch=0;
    while(ch!=5)
    {
        cout<<"\n1.CREATE";
        cout<<"\n2.INSERT";
        cout<<"\n3.DELETE";
        cout<<"\n4.DISPLAY";
        cout<<"\n5.EXIT\n";
        cin>>ch;


        if(ch==1)
        {
            create();
            cnt++;
            cnt++;
        }

        if(ch==2)
        {
            insert();
            cnt++;
        }
        if(ch==3)
        {
            del();
            cnt--;
        }

        if(ch==4)
        {
            display();
        }

        if(ch==5)
        {
            break;
        }
    }
}