Wednesday, 5 June 2013

Stack program in Data Structure



#include<iostream.h>
#include<conio.h>
class stack
{
int data;
stack *next,*prev;
public:
void push(int);
int pop();
void display();
};
stack *bottom,*top;

/* push function*/
void stack::push(int num)
{
if((bottom==NULL)&&(top==NULL))
{
top=bottom=new stack;
top->data=num;
top->next=NULL;
top->prev=NULL;
}
else
{
top->next=new stack;
top->next->data=num;
top->next->prev=top;
top->next->next=NULL;
top=top->next;
}
}
/* display  function*/

 void stack:: display()
{       
 stack *q=bottom;
 if(bottom==NULL)
 {
 cout<<"\n\nStack is empty";
 }
 else {
 while(q!=NULL)
{
cout<<"\t "<<q->data;
q=q->next;
}
}
}
/*pop  function*/
int stack::pop()
{     int result;
if((bottom==NULL)&&(top==NULL))
{
return -1;
}
result=top->data;
top=top->prev;

if(top!=NULL)
{
delete(top->next);
top->next=NULL;
return result;
}
else
{
delete(bottom);
bottom=NULL;
return result;
}
}
/*main()  function*/
void main()
{
stack n1;
int ch;
char c;
do
{    
clrscr();
cout<<"\n 1: PUSH  2: DISPLAY  3: POP";
cout<<"\n\n Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\n Enter Any number: " ;
int num;
cin>>num;
n1.push(num);
break;
case 2:
n1.display();
break;
case 3:
int n=n1.pop();
if(n==-1)
{
cout<<"\n\n Stack is empty";
}
else
{
cout<<"\n\n Popped Item is: "<<n;
}
break;
default:
cout<<"\n wrong selection";
break;
}
cout<<"\n\n Do you want to continue(y/n): ";
cin>>c;
}
while(c=='y');
}




Double Linklist in Data Structure



#include<iostream.h>
#include<conio.h>
class node
{
public:
node *next;
node *prev;
int data;
void add(int num);
void display();
int search(int);
int count();
void sort();
void remove(int);
void insert(int pos,int num);
void Reverse();
};
node *p;

/* add  function*/
void node::add(int num)
{
node *q=p;
if(p==NULL)
{
p=new node;
p->data=num;
p->prev=NULL;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=new node;
q->next->data=num;
q->next->prev=q;
q->next->next=NULL;
}
}

/* insert  function*/
void node::insert(int pos,int num)
{
node *q=p;
node *temp;
if(pos==1)
{
p=new node;
p->data=num;
p->prev=NULL;
p->next=q;
}

if(pos==count()+1)
{
add(num);
}

else{
for(int i=1;i<=(pos-2);i++)
{
q=q->next;
}
temp=q->next;
q->next=new node;
q->next->data=num;
q->next->next=temp;
q->next->prev=q;
q->next->next->prev=q->next;
}
}

/* search  function*/
int node::search(int num)
{      int flag;
int cnt=1;
node *q=p;
if(p==NULL)
{
cout<<"\n List is empty";
}
else
while(q!=NULL)
{
if(q->data==num)
{
flag=1;
break;
}
cnt++;
q=q->next;
}
if(flag==1)
return cnt;
else
return -1;
}
/* Reverse  function*/
void node :: Reverse()
{
node *q=p;
if(p==NULL)
{
cout<<"\n\n List is Empty";
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
while(q!=NULL)
{
cout<<" "<<q->data;
q=q->prev;
}    }
}
/* remove  function*/
void node::remove(int pos)
{
node *q=p;
if(pos==1)
{
cout<<"\n Deleted node's value is: "<< q->data;
p=p->next;
p->prev=NULL;
delete q;
}
else   {
for(int i=1;i<=(pos-2);i++)
{
q=q->next;
}
node *temp;
cout<<"\n Deleted node's value is: "<< q->next->data;
temp=q->next;
q->next=q->next->next;
q->next->prev=q;
delete temp;
}    }

/* sort function*/
void node::sort()
{
node *i=p,*j;
int temp;
for(i=p;i!=NULL;i=i->next)
{
for(j=i;j!=NULL;j=j->next)
{
if(i->data > j->data)
{
temp=i->data;
i->data=j->data;
j->data=temp;
}
   }
 }
cout<<"\n The linked list has been sorted ";
}



/* count function*/
int node::count()
{       int i=0;
node *q=p;
if(p==NULL)
{
return -1;
}
else {
while(q!=NULL)
{
i++;
q=q->next;
}
return i;
}
}
/* display  function*/
void node:: display()
{
node *q=p;
if(p==NULL)
{
cout<<"\n\n Link list is empty";
}
else {
while(q!=NULL)
{
cout<<"\t "<<q->data;
q=q->next;
}
}
}
/* main()  function*/
void main()
{
node n1;
int ch;
char c;
do
{
cout<<"\n 1: ADD  2: DISPLAY  3: COUNT 4: INSERT\n 5: REMOVE 6: REVERSE 7: SEARCH 8: SORT";
cout<<"\n\n Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\n Enter Any number: " ;
int num;
cin>>num;
n1.add(num);
break;
case 2:
n1.display();
break;
case 3:
int cnt=n1.count();
if(cnt==-1)
{
cout<<"\n\n Linked list is empty";
}
else
{
cout<<"\n Number of node is: "<<cnt<<"\n";
}
break;
case 4:
int pos;
cout<<"\n\n Enter position to insert new node: ";
cin>>pos;
if(pos>=1 && pos<=(n1.count()+1))
{
cout<<"\n\n Enter any number: ";
int num;
cin>>num;
n1.insert(pos,num);
}
else
{
cout<<"\n\n Invalid position";
}
break;
case 5:
cout<<"\n\n Enter position to delete node: ";
cin>>pos;
if(pos>=1 && pos<=(n1.count()+1))
{

n1.remove(pos);
}
else
{
cout<<"\n\n Invalid position";
}
break;
case 6:
n1.Reverse();
break;
case 7:
int nu ;
cout<<"\n\n Enter the Number To  Search: ";
cin>>nu;
int n=n1.search(nu);
if(n==-1)
{
cout<<"\n Number Not Found";
}
else
{
cout<<"\n Number found at position: "<<n;
}
break;
case 8:
n1.sort();
break;
default:
cout<<"\n wrong selection";
break;
}
cout<<"\n Do you want to continue(y/n): ";
cin>>c;
}
while(c=='y');
}

Single Linklist in Data Structure



#include<iostream.h>
#include<conio.h>
class node
{
public:
node *next;
int data;
public:
void remove(int);
void sort();
void display();
int count();
int search(int);
void add(int);
void insert(int,int);
};
node *p;

/* add function*/
void node:: add(int num)
{
node *q=p;
if(p==NULL)
{
p=new node;
p->data=num;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=new node;
q->next->data=num;
q->next->next=NULL;
}
}

/* insert function*/
void node::insert(int pos,int num)
{
node *q=p;
node *temp;
int i;
if(pos==1)
{
p=new node;
p->data=num;
p->next=q;
}
if(pos==count()+1)
{
add(num);
}
for(i=1;i<=(pos-2);i++)
{
q=q->next;
}
temp=q->next;
q->next=new node;
q->next->data=num;
q->next->next=temp;
}

/* display function*/
void node::display()
{
node *q=p;
if(p==NULL)
{
cout<<"\nLinked List does not exist";
}
else
{
cout<<"\n";
while(q!=NULL)
{
cout<<"\t"<<q->data;
q=q->next;
}
}
}

/* sort function*/
void node::sort()
{
int temp;
node *i=p;
node *j;
for(i=p;i!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(i->data>j->data)
{
temp=i->data;
i->data=j->data;
j->data=temp;
}
}
}
cout<<"The linked list has been sorted.";
}

/* count function*/
int node::count()
{
int i=0;
node *q=p;
if(p==NULL)
return 0;
else
{
while(q!=NULL)
{
q=q->next;
i++;
}
return i;
}
}

/* search  function*/
int node::search(int num)
{
 node *q=p;
 int pos=1;
 int flag=0;
 while(q!=NULL)
 {
  if(q->data==num)
  {
   flag=1;
   break;
  }
  pos++;
  q=q->next;
 }
 if(flag==1)
 {
  return pos;
 }
 else
 {
  return -1;
 }
}

/* remove function*/

void node::remove(int pos)
{  node *q=p;
node *temp;
if(pos==1)
{
p=q->next;
delete q;
}
for(int i=1;i<=(pos-2);i++)
{
q=q->next;
}
temp=q->next;
q->next=q->next->next;
delete temp;
}

/* main()  function*/
void main()
{
 clrscr();
 p=NULL;
 int num,option;
 char ch;
 node n;
 ch='y';

 while(ch=='y')
 {
  cout<<"\n1.ADD ";
  cout<<"\t   2. DISPLAY   ";
  cout<<"3. SORT      \n";
  cout<<"4. COUNT   ";
  cout<<"5. SEARCH    ";
  cout<<"6. INSERT    \n";
  cout<<"7. REMOVE    " ;
  cout<<"\nEnter your choice : \n";
  cin>>option;
  switch(option)
  {
   case 1:
   {
   cout<<"Enter the Number : ";
   cin>>num;
   n.add(num);
  
   break;
   }
   case 2:
   {
    n.display();
 
   break;
   }
   case 3:
   {
   n.sort();
   break;
   }
   case 4:
   {
   num=n.count();
   cout<<"\nThe Number of Nodes are : "<<num;
   break;
   }
   case 5:
   {
    cout<<"Enter the Number to search : ";
    cin>>num;
    int temp;
    temp=n.search(num);
    if(temp==-1)
    {
     cout<<"\nThe Value is not present in the linked List";
    }
    else
    {
    cout<<"The Number has been found at position : \n"<<temp;
    }
    break;
    }
   case 6:
   { 
   int pos;
   cout<<"\n\n Enter position: ";
   cin>>pos;
   int nn=0;
   nn=n.count();
   if(pos>=1 && pos<=(nn+1))
   {
   int num;
   cout<<"\n Enter number to be insert: ";
   cin>>num;
   n.insert(pos,num);
   }
   else
   {
   cout<<"\n invalid position";
   }
  break;
   }
   case 7:
   {      
   int pos;
   cout<<"\n Enter position: ";
   cin>>pos;
   if(pos>=1 && pos<=(n.count()+1))
   {
    n.remove(pos);
    }
    else
    {
    cout<<"\n Invalid position";
     }
     break;
     }
   default:
    {
    cout<<"Invalid Selection";
    }
    }
   cout<<"\n\n Do you want to continue(y/n): ";
   cin>>ch ;
   }
   }