2011-11-11 13 views
0

リンクリストを使用してスタックを作成するプログラムを作成しています。私はすべての機能をpush()、pop()、top()などのように終えました。スタックから2つのアイテムを削除して値を追加する

私が理解しようとしていることは、スタックから2つの値を取り除き、スタックに入れます。これは割り当ての一部であり、すべてのアイテムが一緒に追加され、合計だけがスタックに残るまで、これを継続しなければなりません。

ご意見やご感想をお寄せください。

編集:別の機能を作って問題を解決しました。 お手伝いしてくださった皆様、ありがとうございます!ここで

は私のコードです:

#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

あなたは何をする必要があるようです:2つのポップ、追加、そしてプッシュ。あなたはそれを違うようにしなければなりませんか?またはむしろ、何が助けを必要としますか? –

+0

私の問題は、各pop()の後に一時的な値を削除した場合、私が既にポップした値にどのようにアクセスするのか分からないことだと思います。 – Blake

+0

あなたのスタックに合計を戻すだけですか? –

答えて

2

は、あなたが持っている必須インタフェースですか?通常、pop()操作では、単に出力するのではなく、ポップした値(voidではなく)を返すようにします。そうした場合、問題は簡単になり、アルゴリズムを繰り返し使用するだけで、それらを一緒に追加することができます。

実際、pop()、stackTop()、stackSize()、およびisEmpty()はおそらくすべてそれらの値を返すべきです。関数内でprintステートメントが必要ない場合は、主プログラムに見つかった値を出力させることができます。

関連する問題