// 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;
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.
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.
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
Bookmarks