2011-11-11 21 views
0

我正在編寫一個使用鏈接列表創建堆棧的程序。我完成了所有功能,如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; 
} 
+0

你似乎得到了你需要做的事情:兩個流行音樂,一個添加,然後推動。你需要改變它嗎?或者說,你需要什麼幫助? –

+0

我想我的問題是我不知道我將如何訪問我已經彈出的值,如果我刪除每個流行()後的臨時值。 – Blake

+0

只是推回到您的堆棧? –

回答

2

是你的界面強制性?通常你會讓你的pop()操作返回剛剛彈出的值(而不是無效),而不是僅僅打印它。如果你這樣做,你的問題變得簡單,你可以重複使用你的算法將它們加在一起。事實上,pop(),stackTop(),stackSize()和isEmpty()應該都可以返回它們的值。如果函數中不需要打印語句,那麼可以讓主程序打印它找到的值。

相關問題