Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the C++ “struct” structure to create your SLL nodes. Node position numbering

ID: 3808395 • Letter: U

Question

Use the   C++   “struct” structure to create your SLL nodes. Node position numbering should start with one. The program should include the following options. The program shoudl ask the user to choose an option from the main list below and proceed with the operations

InitStack – Initialize LIFO Stack

PushStack – Add an item to top of Stack at

PrintStack – Print out the Stack items

PopStack – Remove and return top item from Stack

TopStack – Returns the first item on Stack without deleting it

EmptyStack – Checks for empty Stack condition (TRUE/FALLS)

ClearStack – Delete all items from Stac

Explanation / Answer

#include<iostream>
using namespace std;


//   Creating a NODE Structure
struct node
{
    int data;
    struct node *next;
};

// Creating a class STACK
class stack
{
    struct node *top;
    public:
    stack() // constructure
    {
        top=NULL;
    }
    void push(); // to insert an element
    int pop(); // to delete an element
    int Top();//returns top element of stack
    void show(); // to show the stack
    bool Emptystack();//to check whether stack is empty or not
    void clearstack();//deleting all elements of stack
};

int stack::Top()
{
   if(top==NULL)return -1;
   return top->data;//returning top element without deleting it
  
}
// PUSH Operation
void stack::push()
{
    int value;
    struct node *ptr;
    cout<<"nPUSH Operationn";
    cout<<"Enter a number to insert: ";
    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!!!";

}

// POP Operation
bool stack::Emptystack()
{
  
   if(top==NULL)
   return true;//returns true if stack is empty
   return false;//otherwise returns false
}
void stack::clearstack()
{
   while(top!=NULL)
   pop();
   //clearing stack
}
int stack::pop()
{
    struct node *temp;
    if(top==NULL)
    {
        cout<<"nThe stack is empty!!!";
    }
    temp=top;
    top=top->next;
    cout<<"nPOP Operation........nPoped value is "<<temp->data;
    int d = temp->data;
    delete temp;
    return d;//returning value
}

// Show stack
void stack::show()
{
    struct node *ptr1=top;
    cout<<"nThe stack isn";
    while(ptr1!=NULL)
    {
        cout<<ptr1->data<<" ->";
        ptr1=ptr1->next;
    }
    cout<<"NULLn";
}

// Main function
int main()
{
    stack s;
    int choice;
    while(1)
    {
        cout<<"n-----------------------------------------------------------";
        cout<<" ttSTACK USING LINKED LIST ";
        cout<<" 1:PUSH stack 2:POP stack 3:DISPLAY STACK 4:Topstack 5:emptystack 6:clear stack 7:EXIT ";
        cout<<"nEnter your choice(1-4): ";
        cin>>choice;
        switch(choice)
        {
            case 1:
                s.push();
                break;
            case 2:
                cout<<" Removed element :"<<s.pop()<<endl;
                break;
            case 3:
                s.show();
                break;
            case 4:
               cout<<" Top element of stack :"<<s.Top()<<endl;
               break;
           case 5:
               if(s.Emptystack())
               cout<<" Empty stack : "<<"True"<<endl;
               else
               cout<<" Empty stack : "<<"False"<<endl;
               break;
           case 6:
               s.clearstack();
               break;
            case 7:
                return 0;
                break;
            default:
                cout<<"Please enter correct choice(1-4)!!";
                break;
        }
    }
    return 0;
}

output:-

n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 1
nPUSH OperationnEnter a number to insert: 1
nNew item is inserted to the stack!!!n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 1
nPUSH OperationnEnter a number to insert: 2
nNew item is inserted to the stack!!!n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 1
nPUSH OperationnEnter a number to insert: 3
nNew item is inserted to the stack!!!n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 1
nPUSH OperationnEnter a number to insert: 4
nNew item is inserted to the stack!!!n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 3
nThe stack isn4 ->3 ->2 ->1 ->NULLnn-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 2
nPOP Operation........nPoped value is 4
Removed element :4
n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 4

Top element of stack :3
n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 3
nThe stack isn3 ->2 ->1 ->NULLnn-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 5

Empty stack : False
n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 6
nPOP Operation........nPoped value is 3nPOP Operation........nPoped value is 2nPOP Operation........nPoped value is 1n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 5

Empty stack : True
n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 4

Top element of stack :-1
n-----------------------------------------------------------
ttSTACK USING LINKED LIST


1:PUSH stack
2:POP stack
3:DISPLAY STACK
4:Topstack
5:emptystack
6:clear stack
7:EXIT
nEnter your choice(1-4): 7


Process exited normally.
Press any key to continue . . .