Я пишу программу, которая создает стек, используя связанные списки. У меня есть все функциональные возможности, такие как push (), pop (), top () и т. Д.
Я пытаюсь выяснить, как удалить два значения из стека, сложить их вместе и вставить в стек. Это часть задания, и мы должны продолжать делать это, пока все элементы не будут сложены вместе, и в стеке останется только сумма.
Любая помощь или советы будут оценены!
РЕДАКТИРОВАТЬ: я решил свою проблему, просто сделав другую функцию!
Спасибо всем, кто пытался помочь!
Вот мой код:
#include <cstdlib>
#include <iostream>
using namespace std;
//Creating a node structure
struct node
{
int data;
struct node *next;
};
//class stack
class stack
{
struct node *top;
int size;
public:
stack()
{
top=NULL;
}
void push(); // insert an element
void pop(); // delete an element
void stackTop(); //retrive top item without removal
void stackSize(); //return the size of the stack
void isEmpty(); // return 1 if empty, 0 if not
void show(); // show the stack
};
//push items into a stack
void stack::push()
{
int value;
struct node *ptr;
cout << "\nEnter 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!!!" << endl;
size ++;
}
//remove the top item
void stack::pop()
{
struct node *temp;
if(top == NULL)
{
cout<<"\nThe stack is empty!!!";
return;
}
temp = top;
top = top->next;
cout << "\nPoped value is " << temp->data << endl;
delete temp;
size--;
}
//retrive top value without removing it
void stack::stackTop()
{
struct node *temp;
if(top == NULL)
{
cout<<"\nThe stack is empty!!!";
return;
}
temp = top;
cout << "The top item is: " << temp->data << endl;
delete temp;
}
//show the stack
void stack::show()
{
struct node *ptr1 = top;
cout << "\nThe stack is:" << endl;
while(ptr1 != NULL)
{
cout << ptr1->data << " ->";
ptr1 = ptr1->next;
}
cout << "NULL" << endl;
}
//return if empty or not
void stack::isEmpty()
{
if(top == NULL)
{
cout<<"\nThe stack is empty!!!" << endl;
return;
}
else
{
cout << "\nThe stack is not empty!!!" << endl;
return;
}
}
//return the number of items in the stack
void stack::stackSize()
{
if(top == NULL)
{
cout<<"\nThe stack is empty!!!" << endl;
return;
}
else
{
cout << "\nThe stack has " << size << " items" << endl;
return;
}
}
//main function
int main()
{
stack s;
int choice;
while(1)
{
cout << "\nSTACK USING LINKED LIST" << endl << endl;
cout << "1:PUSH" << endl;
cout << "2:POP" << endl;
cout << "3:DISPLAY STACK" << endl;
cout << "4:RETRIVE TOP ITEM" << endl;
cout << "5:GET THE SIZE" << endl;
cout << "6:IS THE STACK EMPTY?" << endl;
cout << "7:EXIT" << endl;
cout << "Enter your choice(1-7): ";
cin >> choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
s.stackTop();
break;
case 5:
s.stackSize();
break;
case 6:
s.isEmpty();
break;
case 7:
exit(0);
break;
default:
cout << "\nPlease enter correct choice(1-7)!!!" << endl;
break;
}
}
return 0;
}