Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

linked stack problem with repeated output

XenoglauxXenoglaux Posts: 5Member
[code]#include
#include
#include
struct node
{
int data;
struct node *link;
};
struct node *top=NULL,*temp;
int main()
{ int choice,data;

while(1)//infinite loop is used to insert/delete infinite number of nodes
{ cout << "
1.Push
2.Pop
3.Display
4.Exit
";
cout << "
Enter ur choice:";
cin >> choice;
switch(choice)
{ case 1:
temp=(struct node *)malloc(sizeof(struct node));
cout << "Enter a node data :";
cin >> data;
temp->data=data;
temp->link=top;
top=temp;

break;

case 2:
if(top!=NULL)
{

cout << "The poped element is " << data,top->data;

top=top->link;
}
else
{ cout << "
Stack Underflow"; }
break;

case 3:
temp=top;
if(temp==NULL)
{ cout << "
Stack is empty
"; }

while(temp!=NULL)
{
cout << "->" <<data << "->",temp->data;
temp=temp->link; }
break;

case 4:

exit(0); }

}
return (0);
}[/code]

when i compile it there's no other single error.
Problems are :
when i push the number 4 5 6 3
and i chose the pop option the output is the poped element is 3 and again i chose pop. the same output appeared.
when i chose display option: the output is 3->3->3->3->

can someone fix the code for me please? i really don't have any clues
Thanks before

Comments

  • aramonkg101aramonkg101 Posts: 12Member
    Well, that you get no errors during compilation means that the syntax and grammar of the code violates no rules. It does not mean that the logic/design of the program is correct.

    Also you have to decide in which language you are going to write the program in. Using malloc(), no classes but employing cin and cout is going to confuse you in the long run.

    Finally not freeing up memory you use through malloc is bad practice and will only lead to problems.

    The problem of repeated output is found somewhere here
    [code]
    temp=(struct node *)malloc(sizeof(struct node));
    cout << "Enter a node data :";
    cin >> data;
    temp->data=data;
    temp->link=top;
    top=temp;
    [/code]

    You always use struct node* top to save your values and you overwrite it. The problem is that with the structure of your program there is no easy way out of it. A corrected version of the code based on your approach is like this.
    [code]
    #include
    #include

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

    int main(int argc, char** argv){
    int choice, data;
    struct node base, *temp;

    base.link = NULL;

    /* Infinite loop is used to insert/delete infinite number of nodes */
    while(1) {
    printf("
    1.Push
    2.Pop
    3.Display
    4.Exit
    ");
    printf("
    Enter ur choice:");
    scanf("%d", &choice);

    switch(choice) {
    case 1:
    temp = &base;
    /* Find the last entry */
    while(temp->link != NULL)
    temp = temp->link;
    /* Allocate memory for the new entry */
    temp->link = (struct node *)malloc(sizeof(struct node));
    printf("Enter a node data :");
    scanf("%d", &data);
    /* The data is inserted in the new entry. NULL is set
    to show that this is the new last entry */
    temp->link->data = data;
    temp->link->link = NULL;
    break;
    case 2:
    if(base.link == NULL){
    printf("
    Stack Underflow
    ");
    }
    else{
    temp = &base;
    /* Finds the last entry but allows to free up memory too */
    while(temp->link->link != NULL)
    temp = temp->link;
    printf("The poped element is %d", temp->link->data);
    free(temp->link);
    /* There is no data at temp->link anymore
    so we set it to NULL */
    temp->link = NULL;
    }
    break;
    case 3:
    if(base.link == NULL){
    printf("
    Stack is empty
    ");
    }
    else{
    temp = base.link;
    /* Find and display all but last entry */
    while(temp->link != NULL) {
    printf("->%d", temp->data);
    temp = temp->link;
    }
    printf("->%d", temp->data);
    }
    break;
    case 4:
    /* Free up memory used by malloc() calls */
    while(base.link != NULL){
    temp = &base;
    while(temp->link->link != NULL)
    temp = temp->link;
    free(temp->link);
    temp->link = NULL;
    }
    exit(0);
    }
    }

    return 0;
    }
    [/code]

    To better design your stack take a look at this
    http://www.cs.bu.edu/teaching/c/stack/linked-list/

    If you want to use C++ consider classes.
Sign In or Register to comment.