-
July 1st, 2009, 06:55 AM
#1
pop and push
anyone help me how to make a program that doing a pop and push
element ?
-
July 1st, 2009, 07:09 AM
#2
Re: pop and push
Do you mean you need some kind of stack ?
Victor Nijegorodov
-
July 2nd, 2009, 08:29 AM
#3
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);
}
-
July 2nd, 2009, 08:33 AM
#4
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.
-
July 2nd, 2009, 11:48 AM
#5
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.
-
July 2nd, 2009, 12:02 PM
#6
Re: pop and push
Originally Posted by obliviator33
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|