RSS

PROGRAM TO DELETE ALL THE ELEMENTS EQUAL TO AN ENTERED ELEMENT FROM THE STACK

29 Mar
// PROGRAM TO IMPLEMENT THE DELETE FUNCTIONALITY IN A STACK

#include <iostream>
#include <cstdlib>
#include <process.h>
#define size 10

using namespace std;

class stack{
	int data[size];
	int top;
	
	public:
		stack()
		{
			top = -1;
		}
		void push();
		void pop();
		void display();
		void del(int);
};

int main()
{
	stack s;
	char wish = 'Y';
	int choice;
	
	do{
		system("cls");
		cout<<"\nImplementation of Delete functionality in Stack....\n\n";
		cout<<"\nPlease enter a choice\n\n";
		cout<<"\n1. PUSH AN ELEMENT\n\n";
		cout<<"\n2. POP AN ELEMENT\n\n";
		cout<<"\n3. DISPLAY THE ELEMENTS OF STACK\n\n";
		cout<<"\n4. DELETE ALL THE ELEMENTS EQUAL TO A CERTAIN ELEMENT FROM THE STACK\n\n";
		cout<<"\n5. EXIT THE PROGRAM\n\n";
		cout<<"\n\nEnter your choice\t";
		cin>>choice;
		
		while(choice<1 || choice >5)
		{
			cout<<"\nInvalid choice.... Enter a valid one below\n\n";
			cout<<"Choice : \t";
			cin>>choice;
		}
		system("cls");
		int var;
		switch(choice)
		{
			case 1: s.push();
					cout<<"\n\nThe stack staus is \n\n";
					s.display();
					break;
			
			case 2: s.pop();
					cout<<"\n\nThe stack staus is \n\n";
					s.display();
					break;
			
			case 3: cout<<"\n\nThe stack staus is \n\n";
					s.display();		
					break;
					
			case 4: cout<<"\n\nEnter the element all elements equal to which should be deleted from stack\n\n";
					cin>>var;
					s.del(var);
					cout<<"\n\nThe stack staus is \n\n";
					s.display();
					break;
					
			case 5: cout<<"\nEXITING FROM THE PROGRAM \n\n";
					exit(0);
					break;	
		}
		
		cout<<"\n\nDo any more operation? (Y/N)";
		cin>>wish;
		
		while(wish != 'y' && wish !='Y' && wish !='n' && wish != 'N')
		{
			cout<<"\n\nInvalid choice \n\n Please enter correct one below...\n";
			cout<<"\nChoice (Y/N): \t";
			cin>>wish;
		}
		
	}while(wish == 'y' || wish == 'Y');
	
	cout<<"\n\nThanks for coming.... Do come again. \n\n";
	cout<<"\nHave a nice day ahead :)";
	
	return 0;
}

void stack::push() 
{
	int element;
	if(top == size-1)
	{
		cout<<"\n\nERROR !!!!!   STACK OVERFLOW !!!!!!\n\n";
		exit(1);
	}
	else
	{
		top++;
		cout<<"\n\nEnter the element to be pushed:\t";
		cin>>element;
		data[top] = element;
	}
	
	cout<<"\n\nINSERTION SUCCESSFUL .....\n\n";
}

void stack::pop()
{
	if(top == -1)
	{
		cout<<"\n\nERROR !!!!!! STACK UNDERFLOW !!!!!!\n\n";
		exit(1);
	}
	else
	{
		int item = data[top];
		top--;
		cout<<"\n\nDeleted element is:\t "<<item;
	}
}

void stack::display()
{
	if(top == -1)
	cout<<"\n\nEMPTY !!!!!\n\n";
	else
	{
		for(int i = top; i >=0; i--)
		{
			if(i == top)
			{
				cout<<"Top -->  "<<data[i]<<'\n';
			}
			else
				cout<<"         "<<data[i]<<'\n';
		}                           
	}
}

void stack::del(int val)
{
	stack tempstack;
	
	for(;top>=0;)
	{
		if(data[top] == val)
		{
			top--;
		}
		else
		{
			//push into temp stack
			tempstack.top++;
			tempstack.data[tempstack.top] = data[top]; 
			top--;
		}
	}
	
	while(tempstack.top>=0)
	{
		top++;
		data[top] = tempstack.data[tempstack.top];
		tempstack.top--;
	}
	
	cout<<"\n\nDeletion Successful !!!!!\n\n";
}
Advertisements
 
5 Comments

Posted by on March 29, 2016 in Stacks, Queues and Linked Lists

 

5 responses to “PROGRAM TO DELETE ALL THE ELEMENTS EQUAL TO AN ENTERED ELEMENT FROM THE STACK

  1. DeNiSkA

    March 31, 2016 at 8:30 am

    hello, i came to this site through learncpp.com (by mistake), and i started reading this above program but i think it has error(i even compiled) the marks you have used for strings are wrong, in C++ : ” and ” are not the same they are different , sthis one(“) is correct and it represents a string so your cout<<….. statements will not execute

    And why are you using system('cls"), i never saw this function and on doing some research i came to know that this is used for clearing user screen in old compliers. And this doesn't execute in code::blocks or visual C++ 2013

    Like

     
    • dikshamodi

      March 31, 2016 at 2:18 pm

      I agree with the above that cout statements won’t execute because of the ” problem. I apologise for that. But it’s because whenever I copy my source code to wordpress, it (WP) automatically converts some characters to other. For example, a — (post/pre decrement) to a – (unary minus).

      The OP can get help with the logic if they want. Hope the logic was clear. Tell me if it wasn’t clear or you had problems with some part(except system(“cls”) which I have addressed below).

      I’ll be happy to make it more clear. 🙂

      Coming to the system(“cls”) part:

      See, the system(“cls”) works in modern compilers. Maybe you got confused with clrscr() and system(“cls”). clrscr() was available in old compilers like turbo c++. system(“cls”) is available in both. Did you include the cstdlib or stdlib.h header file?

      Anyways, this might help you:

      https://support.microsoft.com/en-us/kb/99261

      Hope I was able to help you atleast a little. Thanks for asking, though 🙂

      Like

       
  2. DeNiSkA

    March 31, 2016 at 8:52 am

    This is the fully debugged program and the errors you made where in some line you didn’t close paranthesis for while loop and your special characters like ” –(decrement opperator) were not correct! 🙂

    // PROGRAM TO IMPLEMENT THE DELETE FUNCTIONALITY IN A STACK
    #include
    #include
    #include
    #define size 10
    using namespace std;
    class stack{
    int data[size];
    int top;
    public:
    stack()
    {
    top = -1;
    }
    void push();
    void pop();
    void display();
    void del(int);
    };

    int main()
    {
    stack s;
    char wish = ‘Y’;
    int choice;
    do{
    //system(“cls”);
    cout<<"\nImplementation of Delete functionality in Stack….\n\n";
    cout<<"\nPlease enter a choice\n\n";
    cout<<"\n1. PUSH AN ELEMENT\n\n";
    cout<<"\n2. POP AN ELEMENT\n\n";
    cout<<"\n3. DISPLAY THE ELEMENTS OF STACK\n\n";
    cout<<"\n4. DELETE ALL THE ELEMENTS EQUAL TO A CERTAIN ELEMENT FROM THE STACK\n\n";
    cout<<"\n5. EXIT THE PROGRAM\n\n";
    cout<>choice;

    while(choice5)
    {
    cout<<"\nInvalid choice…. Enter a valid one below\n\n";
    cout<>choice;
    }
    system(“cls”);
    int var;
    switch(choice)
    {
    case 1: s.push();
    cout<<"\n\nThe stack staus is \n\n";
    s.display();
    break;

    case 2: s.pop();
    cout<<"\n\nThe stack staus is \n\n";
    s.display();
    break;

    case 3: cout<<"\n\nThe stack staus is \n\n";
    s.display();
    break;

    case 4: cout<>var;
    s.del(var);
    cout<<"\n\nThe stack staus is \n\n";
    s.display();
    break;

    case 5: cout<<"\nEXITING FROM THE PROGRAM \n\n";
    exit(0);
    break;
    }

    cout<>wish;

    while(wish != ‘y’ && wish !=’Y’ && wish !=’n’ && wish != ‘N’)
    {
    cout<<"\n\nInvalid choice \n\n Please enter correct one below…\n";
    cout<>wish;
    }

    }while(wish == ‘y’ || wish == ‘Y’);

    cout<<"\n\nThanks for coming…. Do come again. \n\n";
    cout<<"\nHave a nice day ahead :)";

    return 0;
    }

    void stack::push()
    {
    int element;
    if(top == size-1)
    {
    cout<<"\n\nERROR !!!!! STACK OVERFLOW !!!!!!\n\n";
    exit(1);
    }
    else
    {
    top++;
    cout<>element;
    data[top] = element;
    }
    cout<<"\n\nINSERTION SUCCESSFUL …..\n\n";
    }

    void stack::pop()
    {
    if(top == -1)
    {
    cout<<"\n\nERROR !!!!!! STACK UNDERFLOW !!!!!!\n\n";
    exit(1);
    }
    else
    {
    int item = data[top];
    top–;
    cout<<"\n\nDeleted element is:\t "<<item;
    }
    }
    void stack::display()
    {
    if(top == -1)
    cout<=0; i–)
    {
    if(i == top)
    {
    cout< “<<data[i]<<'\n';
    }
    else
    cout<<" "<<data[i]<=0;)
    {
    if(data[top] == val)
    {
    top–;
    }
    else
    {
    //push into temp stack
    tempstack.top++;
    tempstack.data[tempstack.top] = data[top];
    top–;
    }
    }

    while(tempstack.top>=0)
    {
    top++;
    data[top] = tempstack.data[tempstack.top];
    tempstack.top–;
    }

    cout<<"\n\nDeletion Successful !!!!!\n\n";
    }

    Like

     
    • dikshamodi

      March 31, 2016 at 2:36 pm

      As I told WP automatically converts characters. Copy paste the code above which you debugged in any IDE of your choice. You will find errors again. Anyways, thanks for taking pains to debug my code. 🙂

      EDIT: I have discovered a way to avoid the automatic conversion. Enclosing the code in code tags. I’ll apply this change to all programs soon.. 🙂

      Like

       
      • DeNiSkA

        March 31, 2016 at 3:50 pm

        haha! you are correct

        Like

         

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: