Coding Lab - TechOnTechnology

Showing posts with label cplusplus. Show all posts
Showing posts with label cplusplus. Show all posts

Program in C++ to generate a Pascal's triangle.




      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1


#include<iostream>
using namespace std;
int fact(int);
main()
{
    int rows,i,j,k;
    cout<<"Enter the numbe of rows you want in the triangle:";
    cin>>rows;
    for(i=0;i<rows;i++)
    {
        //Moving each row by rows-i spaces to get a triangular shape
        for(k=0;k<(rows-i);k++)
        cout<<" ";
        //Loop for printing each row
        for(j=0;j<=i;j++)      
        cout<<" "<<fact(i)/(fact(j)*fact(i-j)); //nCr=n!/(r!*(n-r)!)
        cout<<endl;
    }
}

int fact(int i)
{
    int value=1;   
    while(i!=0)
    {
        value=value*i;
        i--;
    }
    return value;
}

Program in C++ to demonstrate Binary Search Tree.




#include<stdio.h>
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root,*r,*l;
void create(node*);
void preorder(node*);
void postorder(node*);
void inorder(node*);
void main()
{
int choice;
choice=0;
clrscr();
while(choice!=5)
{
    cout<<"\n1.Crete [ Insert ]\n";
    cout<<"2.Preorder\n";
    cout<<"3.Inorder\n";
    cout<<"4.Postorder\n";
    cout<<"5.Quit\n";
    cout<<"Enter your choice [ 1- 5 ] \n";
    cin>>choice;
    switch(choice)
    {
        case 1:    create(root);
            break;
        case 2:    preorder(root);
            break;
        case 3:    inorder(root);
            break;
        case 4: postorder(root);
            break;
        case 5:    choice=5;cout<<"Exiting .. ";
            break;
        default:cout<<"\nInvalid choice !! \n";
    }
}
getch();
}
void create( struct node *temp)
{
    struct node *nw;
    int x;
    nw=new node;
    cout<<"\nEnter element to be inserted :\n";
    cin>>x;
    nw->data=x;
    nw->left=nw->right=NULL;
    if(temp == NULL)
    {
        root=nw;
        l=r=root;
    }
    else if(x>r->data)
    {
        if(r->right != NULL)create(r);
        else{
        r->right=nw;
        r=nw;}
    }
    else if(x<l->data)
    {
        if(r->left != NULL)create(r);
        else{
        l->left=nw;
        l=nw;}
    }
    else
    {
        cout<<"\n"<<x<<" is already exist in binary search tree \n";
    }
}
void preorder(struct node *q)
{
    if(q == NULL)
        cout<<"Error .. EMPTY";
    else
    {
        cout<<q->data<<"  ";
        if(q->left!=NULL)
        preorder(q->left);
        if(q->right!=NULL)
        preorder(q->right);
    }
}
void inorder(struct node *q)
{
    if(q == NULL)
        cout<<"\nError ..EMPTY\n";
    else
    {
        if(q->left!=NULL)
        inorder(q->left);
        cout<<q->data<<"   ";
        if(q->right!=NULL)
        inorder(q->right);
    }
}
void postorder(struct node *q)
{
    if(q == NULL)
        cout<<"\nError.. EMPTY\n";
    else
    {
        if(q->left!=NULL)
        postorder(q->left);
        if(q->right!=NULL)
        postorder(q->right);
        cout<<q->data<<"   ";
    }
}

Program in C++ to demonstrate Merge Sort.




#include<iostream.h>
#include<conio.h>
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
    int mid;
    if(low<high)
    {
        mid=(low+high)/2;
        merge_sort(low,mid);
        merge_sort(mid+1,high);
        merge(low,mid,high);
    }
}
void merge(int low,int mid,int high)
{
    int h,i,j,b[50],k;
    h=low;
    i=low;
    j=mid+1;
    while((h<=mid)&&(j<=high))
    {
        if(a[h]<=a[j])
        {
            b[i]=a[h];
            h++;
        }
        else
        {
            b[i]=a[j];
            j++;
        }
        i++;
    }
    if(h>mid)
    {
         for(k=j;k<=high;k++)
         {
            b[i]=a[k];
            i++;
         }
     }
     else
     {
         for(k=h;k<=mid;k++)
         {
            b[i]=a[k];
            i++;
          }
     }
    for(k=low;k<=high;k++) a[k]=b[k];
}
void main()
{
 int num,i;
 clrscr();
 cout<<"Please Enter the number of elements you have :\n";
 cin>>num;
 cout<<"Please Enter the ( "<< num <<" ) numbers :\n";
 for(i=1;i<=num;i++)cin>>a[i];
 merge_sort(1,num);
 cout<<"The sorted list (using MERGE SORT) will be :\n";
 for(i=1;i<=num;i++)
 cout<<a[i]<<"    ";
 getch();
}

Program in C++ to demonstrate Quick Sort.




#include<iostream.h>
#include<conio.h>
void Display(int* array, int n);
void QuickSort(int* array, int low, int high);
int Partition(int* array, int ItemValue, int low, int high);
void swap(int &a, int &b);
void main()
{
    int i,n,array[50];
    clrscr();
    cout<<"\nHow many numbers you have ?\n";
    cin>>n;
    cout<<"Enter "<<n<<" Values :\n";
    for( i = 0; i < n; i++)
    {cin>>array[i];}
    cout<<"\nThe given list of numbers : \n";
    Display(array,n);
    QuickSort(array,0,n-1);
    cout<<"\nThe sorted list of numbers : \n";
    Display(array,n);
    getch();
}
void swap(int &a, int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}
void Display(int* array, int n)
{
    int i;
    for( i = 0; i < n; i++) cout<<array[i]<<"  ";
}
void QuickSort(int* array, int low, int high)
{
    int Item = array[low];
    int splitPoint;
    if(high > low)
    {
        splitPoint = Partition(array, Item, low, high);
        array[splitPoint] = Item;
        QuickSort(array, low, splitPoint-1);
        QuickSort(array, splitPoint+1, high);
    }
}
int Partition(int* array, int Item, int low, int high)
{
    int L = low;
    int R = high;
    while(L < R)
    {
         while( Item < array[R] && R > L){R--;}
         swap(array[L], array[R]);
         while( Item >= array[L] && L < R){L++;}
         swap(array[R], array[L]);
    }
    return L;
}

Program in C++ to demonstrate Buble Sort.




#include<iostream.h>
#include<conio.h>
void main()
{
    int array[100],n,i,j,temp;
    clrscr();
    cout<<"HOW MANY NUMBERS YOU HAVE  :";
    cin>>n;
    cout<<"ENTER "<<n<<" NUMBERS \n";
    for(i=0;i<n;i++)
        cin>>array[i];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1;j++)
            if(array[j]>array[j+1])
            {
                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
            }
    }
    cout<<"\nArray is sorted in ascending order.\n";
    for(i=0;i<n;i++)
        cout<<array[i]<<"   ";
    getch();
}

Program in C++ to demonstrate Binary Search.




#include <iostream.h>
#include<conio.h>
int BinarySearch(int [], int, int );
void main()
{
    clrscr();
    int n,i,arr[50],item,location;
    cout<<"\nEnter Numbers of elements you have :\n";
    cin>>n;
    cout<<"\nEnter "<<n<<" Elements :\n";
    for(i=0;i<n;i++)cin>>arr[i];
    cout << "Enter the item you are searching for: ";
    cin >> item;
    location = BinarySearch(arr,n,item);
    if (location > -1)
    cout << "The item was found at index location "<< location+1;
    else
    cout << "The item was not found in the list\n";
    getch();
}
int BinarySearch(int list[], int size, int key)
{
    int left, right, midpt;
    left = 0;
    right = size-1;
    while (left <= right)
        {
            midpt = (left + right) / 2;
            if (key == list[midpt])
            {
                return midpt;
            }
            else if (key > list[midpt])
                left = midpt + 1;
            else
                right = midpt - 1;
        }
    return -1;
}

Program in C++ to Convert Infix Expression to Prefix Expression.





#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define size 50
char stack[size];
int tos=0,ele;
void push(int);
char pop();
char infix[30],output[30];
int prec(char);
void main()
{
int i=0,j=0,length;
char temp;
cout<<"\nEnter an infix expression: ";
cin>>infix;
length=strlen(infix);
for(i=0;i<length;i++)
{
    if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!=')' && infix[i]!='(' )
    {
    output[j++]=infix[i];
    }
    else
    {
        if(tos==0)
        {
        push(infix[i]);
        }
        else
        {
        if(infix[i]!=')' && infix[i]!='(')
        {
            if(    prec(infix[i]) <= prec(stack[tos-1])  )
            {
            temp=pop();
            output[j++]=temp;
            push(infix[i]);
            }
            else
            {
            push(infix[i]);
            }
            }
        else
            {
            if(infix[i]=='(')
            {
            push(infix[i]);
            }
            if(infix[i]==')')
            {
            temp=pop();
            while(temp!='(')
            {output[j++]=temp;
            temp=pop();}
            }
            }
            }
            }
}
while(tos!=0)
    {
    output[j++]=pop();
    }
cout<<"The Postfix expression is: "<<output;
getch();
}
void push(int ele)
{
    stack[tos]=ele;
    tos++;
}
char pop()
{
    tos--;
    return(stack[tos]);
}
int prec(char symbol)
{
if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}

Program in C++ to demonstrate Selection Sort.





#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void Selection(int a[],int n);
void main()
{
clrscr();
int arr[100],i,n;
cout<<"\nHOW MANY NUMBER's YOU HAVE ?\n ";
cin>>n;
cout<<"\nENTER "<<n<<" NUMBERS :\n ";
for(i=0;i<n;i++)
{
cin>>arr[i];
cout<<" ";
}
Selection(arr,n);
cout<<"\n\nPress any Key..";
getch();
}
void Selection(int temp[],int p)
{
    int q,small;
    for(int i=0;i<p;i++)
    {
        small=temp[i];
        q=i;
        for(int j=i+1;j<p;j++)
        {
            if(temp[j]<small)
            {
            small=temp[j];
            q=j;
            }
        }
        temp[q]=temp[i];
        temp[i]=small;
    }
    cout<<"\nTHE SORTED NUMBERS USING SELECTION SORT \n";
    for(i=0;i<p;i++)
    {
        cout<<"  "<<temp[i];
    }
}

Program in C++ to demonstrate Circular SinglyLinked List.





#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
    {
    int data;
    node *next;
    node *prev;
    };
node * addnode(node*);
void shownodes(node*);
node *delete_node(node*);
node *head;
void main()
{
    clrscr();
    int ch;
    ch=1;
    head=NULL;
    while( ch != 0)
    {
    cout<<"\nMENU\n";
    cout<<"\t1.ADD A NODE\n";
    cout<<"\t2.DISPLAY THE NODES\n";
    cout<<"\t3.DELETION\n";
    cout<<"\t0.QUIT\n";
    cin>>ch;switch(ch)
        {
        case 1:
            head=addnode(head);break;
        case 2:
            shownodes(head);break;
        case 3:
            head=delete_node(head);break;
        case 0:
            ch=0;break;
        default:
            cout<<"INVALID CHOICE  ?? \n";break;
        }
    }
}
node * addnode(node *f)
{
    node *n;
    int ch;
    n=new node;
    cout<<"ENTER A ELEMENT TO BE INSERTED :";
    cin>>n->data;
    n->next=n->prev=NULL;
    if(f==NULL)
    {f=n;cout<<"\nLIST IS EMPTY.. NODE IS INSERTED AT THE BEGINING..\n";}
    else{
    cout<<"WHERE YOU WANT TO INSERT :\n";
    cout<<"From\n\t1.BEG\n\t2.END\n";
    cin>>ch;
    switch(ch)
    {
    case 1:n->next=f;f->prev=n;f=n;cout<<"SUCCESS\n";break;
    case 2:    node *temp=f;
        while(temp->next != NULL)
        {temp=temp->next;}
        temp->next=n;n->prev=temp;cout<<"SUCCESS \n";break;
    default:cout<<"INVALID ??\n";break;}}
    return f;
}
void shownodes(node *f)
{
    node *temp=f;
    if ( temp == NULL ){
    cout<<endl<<"EMPTY LIST ..\n";
    return;}
    else{
    cout<<"HEAD";
    while(temp!=NULL)
    {cout<<" <=> ";
    cout<<temp->data;temp=temp->next;}
    cout<<" -> NULL "<<endl;}
}
node * delete_node(node *f)
{
    node *temp1,*temp2;int ch;
    temp1=temp2=f;
    if( temp1 == NULL ){
    cout<<"\nEMPTY LIST ... CANNOT BE DELETED \n";}
    else{
    cout<<"From\n\t1.BEG\n\t2.END\n";
    cin>>ch;
    switch(ch){
    case 1:f=f->next;cout<<"SUCCESS\n";break;
    case 2: while(temp1->next!=NULL){
        temp2=temp1;temp1=temp1->next;}
        temp2->next=NULL;
        cout<<"SUCCESS \n";break;
    default:cout<<"INVALID ??\n";break;}}
    return f;
}

Program in C++ to demonstrate Doubly Linked List.




#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
    {
    int data;
    node *next;
    node *prev;
    };
node * addnode(node*);
void shownodes(node*);
node *delete_node(node*);
node *head;
void main()
{
    clrscr();
    int ch;
    ch=1;
    head=NULL;
    while( ch != 0)
    {
    cout<<"\nMENU\n";
    cout<<"\t1.ADD A NODE\n";
    cout<<"\t2.DISPLAY THE NODES\n";
    cout<<"\t3.DELETION\n";
    cout<<"\t0.QUIT\n";
    cin>>ch;switch(ch)
        {
        case 1:
            head=addnode(head);break;
        case 2:
            shownodes(head);break;
        case 3:
            head=delete_node(head);break;
        case 0:
            ch=0;break;
        default:
            cout<<"INVALID CHOICE  ?? \n";break;
        }
    }
}
node * addnode(node *f)
{
    node *n;
    int ch;
    n=new node;
    cout<<"ENTER A ELEMENT TO BE INSERTED :";
    cin>>n->data;
    n->next=n->prev=NULL;
    if(f==NULL)
    {f=n;cout<<"\nLIST IS EMPTY.. NODE IS INSERTED AT THE BEGINING..\n";}
    else{
    cout<<"WHERE YOU WANT TO INSERT :\n";
    cout<<"From\n\t1.BEG\n\t2.END\n";
    cin>>ch;
    switch(ch)
    {
    case 1:n->next=f;f->prev=n;f=n;cout<<"SUCCESS\n";break;
    case 2:    node *temp=f;
        while(temp->next != NULL)
        {temp=temp->next;}
        temp->next=n;n->prev=temp;cout<<"SUCCESS \n";break;
    default:cout<<"INVALID ??\n";break;}}
    return f;
}
void shownodes(node *f)
{
    node *temp=f;
    if ( temp == NULL ){
    cout<<endl<<"EMPTY LIST ..\n";
    return;}
    else{
    cout<<"HEAD";
    while(temp!=NULL)
    {cout<<" <=> ";
    cout<<temp->data;temp=temp->next;}
    cout<<" -> NULL "<<endl;}
}
node * delete_node(node *f)
{
    node *temp1,*temp2;int ch;
    temp1=temp2=f;
    if( temp1 == NULL ){
    cout<<"\nEMPTY LIST ... CANNOT BE DELETED \n";}
    else{
    cout<<"From\n\t1.BEG\n\t2.END\n";
    cin>>ch;
    switch(ch){
    case 1:f=f->next;cout<<"SUCCESS\n";break;
    case 2: while(temp1->next!=NULL){
        temp2=temp1;temp1=temp1->next;}
        temp2->next=NULL;
        cout<<"SUCCESS \n";break;
    default:cout<<"INVALID ??\n";break;}}
    return f;
}


Program in C++ to demonstrate Insertion Sort.




#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int A[50],n,i,j,Temp;
clrscr();
cout<<"\nINSERTION SORT ";
cout<<"\n\nHOW MANY NUMBERS YOU HAVE.: ";
cin>>n;
cout<<"\nENTER THE ELEMENTS OF THE ARRAY...:\n";
for(i=1;i<=n;i++)
{
      cin>>A[i];
}
for(i=2; i<=n; i++)
{
    Temp = A[i];
    j = i-1;
    while(Temp<A[j] && j>=1)
    {
        A[j+1] = A[j];
        j = j-1;
    }
    A[j+1] = Temp;
}
cout<<"\n\nSORTED LIST IN ASCENDING ORDER.:\n";
for(i=1; i<=n; i++)
cout<<A[i]<<"\n";
getch();
}



Program in C++ to demonstrate Singly Linked List.




#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
    {
    int data;
    node *next;
    };
node * addnode(node*);
void shownodes(node*);
node *delete_node(node*);
node *head;
void main()
{
    clrscr();
    int i,ch,a,b,c;
    ch=1;
    head=NULL;
    while( ch != 0)
    {
    cout<<"\nMENU\n";
    cout<<"\t1.ADD A NODE\n";
    cout<<"\t2.DISPLAY THE NODES\n";
    cout<<"\t3.DELETION\n";
    cout<<"\t0.QUIT\n";
    cin>>ch;switch(ch)
        {
        case 1:
            head=addnode(head);break;
        case 2:
            shownodes(head);break;
        case 3:
            head=delete_node(head);break;
        case 0:
            ch=0;break;
        default:
            cout<<"INVALID CHOICE  ?? \n";break;
        }
    }
}
node * addnode(node *f)
{
    node *n;
    int ch;
    n=new node;
    cout<<"ENTER A ELEMENT TO BE INSERTED :";
    cin>>n->data;
    n->next=NULL;
    if(f==NULL)
    {f=n;cout<<"\nLIST IS EMPTY.. NODE IS INSERTED AT THE BEGINING..\n";}
    else{
    cout<<"WHERE YOU WANT TO INSERT :\n";
    cout<<"From\n\t1.BEG\n\t2.SPECIFIED LOCATION\n\t3.END\n";
    cin>>ch;
    switch(ch)
    {
    case 1:n->next=f;f=n;cout<<"SUCCESS\n";break;
    case 2: int pos;node * temp;temp=f;
        cout<<"ENTER THE POSITION  :\n";
        cin>>pos;
        for(int k=1;k<pos-1;k++)
        {if(temp->next == NULL )
        {cout<<"NODES IN THE LIST IS LESS THAN THE POSITION YOU ENTERED..\n";
        return f;}temp=temp->next;}
        n->next=temp->next;temp->next=n;
        cout<<"SUCCESS..\n";break;
    case 3:    temp=f;
        while(temp->next != NULL)
        {temp=temp->next;}
        temp->next=n;cout<<"SUCCESS \n";break;
    default:cout<<"INVALID ??\n";break;}}
    return f;
}
void shownodes(node *f)
{
    node *temp=f;
    if ( temp == NULL ){
    cout<<endl<<"EMPTY LIST ..\n";
    return;}
    else{
    cout<<"HEAD";
    while(temp!=NULL)
    {cout<<" -> ";
    cout<<temp->data;temp=temp->next;}
    cout<<" -> NULL "<<endl;}
}
node * delete_node(node *f)
{
    node *temp1,*temp2;int ch;
    temp1=temp2=f;
    if( temp1 == NULL ){
    cout<<"\nEMPTY LIST ... CANNOT BE DELETED \n";}
    else{
    cout<<"From\n\t1.BEG\n\t2.SPECIFIED LOCATION\n\t3.END\n";
    cin>>ch;
    switch(ch){
    case 1:f=f->next;cout<<"SUCCESS\n";break;
    case 2: int pos;node * temp;temp=f;
        cout<<"ENTER THE POSITION  :\n";
        cin>>pos;
        for(int k=1;k<pos-1;k++)
        {if(temp->next == NULL )
        {cout<<"NODES IN THE LIST IS LESS THAN THE POSITION YOU ENTERED..\n";
        return f;}temp=temp->next;}
        temp->next=temp->next->next;
        cout<<"SUCCESS..\n";break;
    case 3:while(temp1->next!=NULL){
        temp2=temp1;temp1=temp1->next;}
        temp2->next=NULL;
        cout<<"SUCCESS \n";break;
    default:cout<<"INVALID ??\n";break;}}
    return f;
}


Program in C++ to demonstrate Queue using Linked Lists.




#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
class Que
{
struct node *front,*rear;
public:
Que(){front=rear=NULL;}
void ins();
void del();
void show();
};
void Que::ins()
{
int value;
struct node *ptr;
cout<<"\nEnter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(front==NULL)
front=ptr;
else
rear->next=ptr;
rear=ptr;
cout<<"\nSucess!!!";
}
void Que::del()
{
if(front==NULL)
{
cout<<"\nQueue is empty!!";
return;
}
struct node *temp;
temp=front;
front=front->next;
cout<<"\nDeleted value is "<<temp->data;
}
void Que::show()
{
struct node *ptr1=front;
if(front==NULL)
{
cout<<"\nThe Queue is empty!!";
return;
}
cout<<"\nThe Queue is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL";
}
void main()
{
clrscr();
Que q;
int choice;
while(choice!=4)
{
cout<<"\n\nMENU\n";
cout<<"1:INSERTION\n2:DELETION\n3:VIEW\n4:QUIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:q.ins();break;
case 2:q.del();break;
case 3:q.show();break;
case 4:choice=4;break;
default:cout<<"\nPlease enter correct choice(1-4)!!";break;
}
}
}





Program in C++ to demonstrate Queue Using Arrays.




#include<conio.h>
#include<iostream.h>
# define SIZE 150
class Que
{
int front,rear;
int que[SIZE];
public:
Que(){
front=-1;
rear=-1;
}
void ins();
void del();
void disp();
};
void Que::ins()
{
    if(rear==(SIZE-1))
    {
        cout<<"\nQueue is full! OVERFLOW ..";
    }
    else
    {
        int item;
        cout<<"\nEnter element to be inserted : ";
        cin>>item;
        if(front==(-1) && rear==(-1))
        {rear=0;front=0;}
        else
        rear=rear+1;
        que[rear]=item;
        cout<<"\nSuccess..\n";
    }
}
void Que::del()
{
    if(front != (-1))
    {
        cout<<"\n"<<que[front]<<" is deleted ..\n";
        if(front==rear)
            front=rear=(-1);
        else
            front=front+1;
    }
}
void Que:: disp()
{
    int i;
    if(front==(-1))
        {cout<<"\nQueue is empty. \n";}
    else
    {
        cout<<"\nThe queue is :\n";
        for(i=front;i<=rear;i++)
            cout<<que[i]<<" ";
            cout<<"\n";
    }
}
void main()
{
    clrscr();
    int element,choice;
    Que q;
    clrscr();
    while(choice!= 4)
    {
        cout<<"\nMENU \n1: INSERT.\n2: REMOVE.";
        cout<<"\n3: VIEW\n4: QUIT";
        cout<<"\nEnter your choice: ";
        cin>>choice;
        switch(choice)
        {
            case 1:    q.ins();q.disp();break;
            case 2:    q.del();q.disp();break;
            case 3: q.disp();break;
            case 4: choice=4;cout<<"Press any key..";break;
            default:cout<<"Please re-enter your choice.";break;
         }
    }
getch();
}

Program in C++ to demonstrate Stack Using Linked List.



#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int data;  
struct node *next;
};
class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void show();
};
void stack::push()
{
int value;
struct node *ptr;
cout<<"Enter a number to be inserted: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
}
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nTHE STACK IS EMPTY !!!";
return;
}
temp=top;
top=top->next;
cout<<"\n\n The popped value is.. "<<temp->data;
}
void stack::show()
{
if(top == NULL ){
cout<<"\nSTACK IS EMPTY..!!";
return;
}
else{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}}
void main()
{
clrscr();
stack s;
int choice;
choice=0;
while(choice!=4){
cout<<"\n\nSTACK USING LINKED LIST\n";
cout<<"1:PUSH\n2:POP\n3:VIEW\n4:QUIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:s.push();break;
case 2:s.pop();break;
case 3:s.show();break;
case 4:choice=4;break;
default:cout<<"\nINVALID COICE !! [ 1-4 ]";break;
}

}
}

Program in C++ to demonstrate Stack Using Arrays.





#include<stdio.h>
#include<iostream.h>
#include<conio.h>
class Stack
{
int top;
int arr[50];
public:
    Stack(){top=-1;}
    void push();
    void pop();
    void view();
    int isEmpty();
    int isFull();
};
int Stack::isEmpty(){return (top==(-1)?1:0);}
int Stack::isFull(){return ( top == 50 ? 1 : 0 );}
void Stack::push()
{
    if(isFull())
    {cout<<"\nSTACK IS FULL { OVERFLOW }";}
    else
    {
        int i;
        cout<<"\nEnter an element :\n";
        cin>>i;++top;arr[top]=i;
        cout<<"\nInsertion successfull.";
    }
}
void Stack::pop()
{
    int num;
    if(isEmpty()){cout<<"\n STACK IS EMPTY [ UNDERFLOW ] ";}
    else
    {
    cout<<"Deleted item is : "<<arr[top];top--;
    }
}
void Stack::view()
{
    if(isEmpty()){cout<<"\n STACK IS EMPTY [ UNDERFLOW ] ";}
    else
    {
    cout<<"\nSTACK :\n";
    for(int i=top;i>=0;i--){cout<<arr[i]<<"\n";}
    }
}
void main()
{
Stack s;
int ch;
clrscr();
ch=0;
while(ch!=3)
{
cout<<"\nMENU : \n";
cout<<"0. PUSH\n";
cout<<"1. POP\n";
cout<<"2. VIEW\n";
cout<<"3. QUIT\n";
cin>>ch;
switch(ch)
{
case 0:s.push();break;
case 1:s.pop();break;
case 2:s.view();break;
case 3:ch=3;cout<<"Press any key .. ";break;
default:cout<<"INVALID CHOICE  !! \n";break;
}
}
getch();
}

Name

Email *

Message *