pop and push
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: pop and push

  1. #1
    Join Date
    Jun 2009
    Posts
    16

    pop and push

    anyone help me how to make a program that doing a pop and push
    element ?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,004

    Re: pop and push

    Do you mean you need some kind of stack ?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2009
    Posts
    16

    Re: pop and push

    i have here my code but i cannot display the top of the element

    anyone look
    and try to help me ...

    - - -- - - - - - - - -- -- - - - - -- - - - - -

    #include<iostream>
    #include<windows.h>
    using namespace std;

    //struct

    struct node
    { int Elem; // element

    node *nxt; // Pointer to next node

    };

    node *start_ptr = NULL;


    //function declaration
    void asking(char);//function
    void Push ();//function
    void Pop();//function


    char Option;
    int main()
    {

    asking(Option);




    return 0;
    }



    void Push()
    {
    node *temp, *temp2; // pointer

    // Reserve space and fill in for new node
    temp = new node;
    cout << "Please enter element: ";
    cin >> temp->Elem;
    temp->nxt = NULL;

    // Set up link to node
    if (start_ptr == NULL)
    start_ptr = temp;
    else
    {
    temp2 = start_ptr;
    // is not NULL - list not empty!
    while (temp2->nxt != NULL)
    { temp2 = temp2->nxt;
    // Move - next link
    }
    temp2->nxt = temp;
    }



    temp = start_ptr;
    do
    { if (temp == NULL)
    cout << "End of list" << endl;
    else
    { // Display details for what temp points to
    cout << "element : " << temp->Elem << endl;
    cout << endl; // Blank line

    // Move to next node (if present)
    temp = temp->nxt;
    }
    }
    while (temp != NULL);

    asking(Option);

    }



    void Pop() //function pop - this function will pop the element entered
    { node *temp1, *temp2;
    if (start_ptr == NULL)
    cout << "\n\n\n +*+*+*+*+*+* The list is empty! +*++*+*+*+*+*\n\n\n" << endl;
    else
    { temp1 = start_ptr;
    if (temp1->nxt == NULL) // This part is new!
    { delete temp1;
    start_ptr = NULL;
    }
    else
    { while (temp1->nxt != NULL)
    { temp2 = temp1;
    temp1 = temp1->nxt;
    }
    delete temp1;
    temp2->nxt = NULL;
    }
    }



    asking(Option);

    }



    void asking(char Option)
    {

    cout<<"+*+*+*+*+**+___OPTION___+*+*+*+*+*+*"<<endl;
    cout<<"(A.)+*+*+* PUSH +*+*+*+"<<endl;
    cout<<"(B.)+*+*+* POP +*+*+*"<<endl;
    cout<<"(C.)+*+*+* TOP *+*+*+"<<endl;
    cout<<"(D.)+*+*+* QUIT *+*+*+*"<<endl<<endl;
    cout<<"\t\t\t /-/-/-/choose option /-/-/-/:";
    cin>>Option;

    system ("cls");
    do{


    if(Option == 'a' || Option == 'A')
    {
    Push();
    }
    else if(Option == 'b' || Option == 'B')
    {

    Pop();
    }
    else if (Option== 'c' || Option == 'C')
    {

    }
    else if (Option == 'd' || Option == 'D')
    {
    cout<<"\n\n\n\n *+*+*+*+*+*+*+ thank you *+*+*+*+*+*+*+* \n\n\n\n";
    }


    else
    cout<<"\n\n\n *+*+**+*+*+*+*+*+*+ invalid +*+*+*+*+*+*+*+**+ \n\n\n";



    }while(Option =0);


    }

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: pop and push

    I guess you are in the same class as this person ?

    start_ptr is the first node... I assume that this is the top of your stack. Also if you change your linked list to a double linked list, you can move UP and down through the list. Simply add a 'node *prev' to your node struct that points to the previous item.

  5. #5
    Join Date
    Jul 2009
    Posts
    9

    Re: pop and push

    They wouldn't really need to know anything other than the top, which is the point of the stack, so making a doubly linked list would probably confuse him more than it helps him.

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: pop and push

    Quote Originally Posted by obliviator33 View Post
    They wouldn't really need to know anything other than the top, which is the point of the stack, so making a doubly linked list would probably confuse him more than it helps him.
    hmm... yeah.. it could be confusing, didn't think of that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center