2017-07-20 1 views
-4

Ich habe den Algorithmus von Einführung in den Algorithmus untersucht und dann Ich habe diesen Code geschrieben. Aber in meiner Ausgabe zeigt ein anderer Wert für den Index 0 und wenn ich Pop-Funktion verwenden es 1 Anzeige anstelle von 3C++ Implementierung von Stack mit Array

#include <iostream> 

int top; 
void initialise_top(){ 
top = -1; 
} 

bool stack_empty(int a[]){ 
if(top == -1) 
    return true; 
else 
    return false; 
} 

void push(int a[], int x, int s){ 
if(top < s - 1){ 
top = top + 1; 
a[top] = x; 
} 
else 
    std::cout << "overflow" << "\n"; 
} 

int pop(int a[]){ 
if (stack_empty(a) == true) 
    std::cout << "Underflow" << "\n"; 
else{ 
    --top; 
    return a[top+1]; 
} 
} 

void display(int a[]){ 
    for(int i = 0;i <= top; i++){ 
    std::cout << a[i] << " "; 
    } 
} 
int main() 
{ 
    int arr[7]; 
    push(arr,15,7); 
    push(arr,6,7); 
    push(arr,2,7); 
    push(arr,9,7); 
    push(arr,17,7); 
    push(arr,3,7); 
    display(arr); 
    std::cout << "\n"; 
    int out = pop(arr); 
    std::cout << pop << "\n"; 

    return 0; 
} 

Hier wird die Momentaufnahme der Ausgabe enter image description here

+0

Was haben Sie beim Durchlaufen Ihres Codes mit dem Debugger beobachtet? – user0042

+0

Es sagt "Ziel ist auf dem neuesten Stand. Nichts zu tun (alle Elemente sind auf dem neuesten Stand)." – coder

+0

Das ist eine Nachricht von Ihrem Buildsystem. Weißt du, was ein Debugger überhaupt ist? – user0042

Antwort

0

ich diesen Stapel-Array-Code haben in C. Sie können es als Leitfaden bei der Implementierung in C++ verwenden.

#include <stdio.h> 
#include <stdlib.h> 

void push(void); 
void pop(void); 

int a[5]; 
int top = -1; 
int counter = 0; 
int choice; 


main() { 

do{ 
    printf("*********************************************\nSTACK\nPress the 
corresponding button you desire.\n\nPress 1 to push a number to 
stack.\nPress 2 to display the current stack.\nPress 3 to pop the current 
stack.\nPress 0 to exit.\n\n"); 
    scanf("%d", &choice); 
    if(choice == 0){ 
     choice = 0; 
    } 
    else if(choice == 1){ 
     push(); 
    } 
    else if(choice == 2){ 
     int i; 
    printf("Current Stack:\n"); 
    for(i = 0;i <= 4;i++){ 
     printf("%d", a[i]); 
    } 
    printf("\n\n"); 
    } 
    else if(choice == 3){ 
     pop(); 
    } 
}while(choice != 0); 


} 

void push(){ 

    if(top <= 3){ 
    int input; 
    printf("Enter number to push: "); 
    scanf("%d", &input); 
    top = top + 1; 
    a[top] = input; 

    int i; 
    printf("Current Stack:\n"); 
    for(i = 0;i <= 4;i++){ 
     printf("%d", a[i]); 
    } 
    printf("\n\n"); 
    }else{ 
    printf("Out of Bounds\n\n"); 
    exit(0); 
    } 
} 

void pop(){ 
    if(top >= 0){ 
    printf("You just popped: "); 
    printf("%d \n\n", a[top]); 
    a[top] = 0; 

    printf("Current Stack:\n"); 
    int i; 
    for(i = 0;i <= 4;i++){ 
     printf("%d", a[i]); 
    } 
    printf("\n\n"); 
    top = top - 1; 
    }else{ 
    printf("Out of Bounds\n\n"); 
    exit(0); 
    } 

} 
1

In Ihrer Implementierung haben Sie die Funktion "initialise_top()".

void initialise_top(){ 
    top=-1; 
} 

Aber Sie es nicht in Haupt-Funktion aufrufen. Wenn Sie es nicht aufrufen, können Sie die Variable "top" nicht initialisieren, und die Variable "top" enthält den Wert "marbage". Sie können Details lesen hier: Default variable value

Und auch in diesem Fall entweder Zeilen, die Sie einige Fehler haben:

int out=pop(arr); 
std::cout<<pop<<"\n"; 

müssen Sie drucken "out" Variable:

std::cout << out << "\n"; 

Sie suchen können korrigierter Code für Ihre Implementierung hier:

https://repl.it/JaOd/0

0
#include <iostream> 

int top; 
void initialise_top(){ 
top=-1;} 

bool stack_empty(int a[]){ 
if(top==-1) 
return true; 
else 
return false; 
} 

void push(int a[],int x,int s){ 
if(top<s-1){ 
top=top+1; 
a[top]=x; 
} 
else 
std::cout<<"overflow"<<"\n"; 
} 

int pop(int a[]){ 
if (stack_empty(a)==true) 
std::cout<<"Underflow"<<"\n"; 
else{ 
--top; 
return a[top+1]; 
} 
} 

void display(int a[]){ 
    for(int i=0;i<=top;i++){ 
    std::cout<<a[i]<<" "; 
} 
} 
int main() 
{ 
    **initialise_top();**//this statement initialises top=-1 
    int arr[7]; 
    //std::cout<<stack_empty(arr)<<"\n"; 
    push(arr,15,7); 
    push(arr,6,7); 
    push(arr,2,7); 
    push(arr,9,7); 
    push(arr,17,7); 
    push(arr,3,7); 
    display(arr); 
    std::cout<<"\n"; 
    int out=pop(arr); 
    std::cout<<**out**<<"\n"; 
    return 0; 
} 

1.In Ihrem Programm den Wert von top = 1, wenn das erste Element 15 eingefügt wird. fällig zu diesem wird ein anderer Wert für den Index 0 angezeigt. Um top = 0 zu haben, rufen Sie die Funktion initialise_top(); in der Hauptfunktion auf. 2.Um 3 anstelle von 1 anzuzeigen, verwenden Sie std::cout<<out<<"\n"; Änderungen im Programm sind fett gedruckt.

0

Ich habe versucht, meinen Code zu verbessern. Bitte sag mir, ob sich das verbessern kann.

#include <iostream> 

#define max 1000 
class Stack{ 
    int top; 

public: 
    int a[max]; 
    Stack(){ 
     top=-1; 
     } 
     bool stack_empty(); 
     void push(int x); 
     int pop(); 
     void display(); 
}; 

bool Stack::stack_empty(){ 
if(top==-1) 
    return true; 
else 
    return false; 
} 

void Stack::push(int x){ 
    int s=max-1; 
if(top<s){ 
top=top+1; 
a[top]=x; 
} 
else 
    std::cout<<"overflow"<<"\n"; 
} 

int Stack::pop(){ 
if (stack_empty()==true) 
    std::cout<<"Underflow"<<"\n"; 
else{ 
    --top; 
    return a[top+1]; 
} 
} 

void Stack::display(){ 
for(int i=0;i<=top;i++){ 
    std::cout<<a[i]<<" "; 
} 
} 

int main() 
{ 
    Stack stack1; 
    stack1.push(15); 
    stack1.push(6); 
    stack1.push(2); 
    stack1.push(9); 
    stack1.push(3); 
    stack1.display(); 
    std::cout<<"\n"; 
    std::cout<<stack1.pop()<<"\n"; 
    stack1.display(); 
    return 0; 
} 
Verwandte Themen