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

Create a data structure called “Staque” which can store only integers. Create a

ID: 3680857 • Letter: C

Question

Create a data structure called “Staque” which can store only integers. Create a Staque.h, Staque.cpp and a driver file in which you will test the Staque class. The way Staque works is as follows:

1) If the number that you are trying to store in the Staque is even, it is pushed in front of the Staque

2) If the number that you are trying to store in the Staque is odd, it is pushed at the end of the Staque

3) When you try to remove a number from the Staque, you always do it from either the front or from the back of the Staque following the LIFO rule.

Write a C++ code to implement a Staque. Since the data structure is all about inserting and deleting numbers, it would be a good option to use a linked list to implement Staque.

Here’s how your user interface should like: Insert the numbers 1, 3, 2, 4, 6, 8 9 in the Staque.

Display the Staque: This is how the Staque will look like given that the above numbers were pushed in the Staque in the order given above: (front) 8 6 4 2 1 3 9 (back)

Delete 2 even numbers and 1 odd number from the Staque and then display the Staque:

Since deletion always follows the LIFO order, the numbers to be removed are 8 first and then 6(the 2 even numbers) and 9(odd) from the back of the Staque. The Staque shall then look like: (front) 4 2 1 3 (back).

Run you program for at least 3 different input series & corresponding 3 different removal series.

Explanation / Answer

stack.cpp

#include<iostream>

using namespace std;

struct node
{
int data;
struct node *next;
};

class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void display();
};

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!!!";

}

void 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;
delete temp;
}

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

int main()
{
stack s;
int choice;
while(1)
{
cout<<"n-----------------------------------------------------------";
cout<<"nttSTACK USING LINKED LISTnn";
cout<<"1:PUSHn2:POPn3:DISPLAY STACKn4:EXIT";
cout<<"nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
return 0;
break;
default:
cout<<"Please enter correct choice(1-4)!!";
break;
}
}
return 0;
}

OUTPUT:

STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1

PUSH Operation
Enter a number to insert: 8

New item is inserted to the stack!!!
-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1

PUSH Operation
Enter a number to insert: 6

New item is inserted to the stack!!!
-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1

PUSH Operation
Enter a number to insert: 4

New item is inserted to the stack!!!
-----------------------------------------------------------
STACK USING LINKED LIST
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1

PUSH Operation
Enter a number to insert: 2
New item is inserted to the stack!!!
-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1

PUSH Operation
Enter a number to insert: 1
New item is inserted to the stack!!!
-----------------------------------------------------------
STACK USING LINKED LIST
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1

PUSH Operation
Enter a number to insert: 3
New item is inserted to the stack!!!
-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1

PUSH Operation
Enter a number to insert: 9
New item is inserted to the stack!!!
-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 2
(front)
POP Operation........
Poped value is 8
-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4):2
(front)
POP Operation...........
Poped value is 6

-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 2
(back)
POP Operation..............
Poped value is 9
-----------------------------------------------------------
STACK USING LINKED LIST
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 3

The stack is

(front)4 2 1 3(back)
-----------------------------------------------------------
STACK USING LINKED LIST
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote