#pragma hdrstop //Doing nothing here
#pragma argsused //Lets you to dwell in ignorance about poorly written code
#include <tchar.h> //Includes windows specific header
#include <stdio.h> //Includes deprecated header
#include <iostream.h> //Includes deprecated header. Use non-h versions, Luke!
#include <conio.h> //Includes ancient, deprecated, non-standard, awful header
#include <string> //Includes string header
//Linefeed
struct node; //Forward declaration of struct node
typedef node *nodeptr; //declares that nodeptr ia actually node* type
struct node //Defines structure node
{ //Opening brace
string data; //Field "data" of type string (shouldn't compile)
nodeptr next; //Field "next" of type nodeptr (as we know it actually node*)
}; //Closing brace and semicolon
//Linefeed
nodeptr top; //Global variable "top" of type nodeptr (node*)
//Linefeed
void pushstack(string data) //defines function pushstack() with one argument of type
//string which have local name "data"
{ //<- S.O.B (Start of block)
nodeptr temp; //Variable "temp" of type nodeptr (node*)
temp = new node; //Assign to "temp" address of newly created node
temp->data = data; //assign copy of "data" argument to "data" member variable
//of struct "temp" points to
temp->next = top; //Makes "next" member variable of struct "temp" points to
//to point to the struct "top" points to
top = temp; //Makes "top" global variable points to same node "temp" points to
} //EOF (end of function)
//Linefeed
string popstack() //defines function popstack() which returns string (will not compile)
{ //Function start!
if(top == NULL) return""; //If "top" variable equals to NULL, return empty string;
nodeptr temp; //Variable "temp" has type "nodeptr" (node*)
string data = top->data; //variable "data" has type string and contains
//content of "data" field of node struct "top" points to
temp = top; //makes temp point to the same memory area top pointing to
top = top->next; //Makes "top" points to the node "next" field of top node points to
delete temp; //Deletes node "temp" points to (old "top")
return data; //Return copy of the variable "data" to the calling function
} // Captain Obvious to the rescue: "This is an end of a function"
//Linefeed
int _tmain(int argc, _TCHAR* argv[]) //Violating standard
{ //program start
top = NULL; //Initializating "top" global variable with NULL (0)
string data; //Variable "data" of type string (will not compile)
char pilih; //Variable "pilih" of type char
do { //We are repeating next five lines ...
cout << "Input data > ";//Outputting "Input data > "
cin >> data; //Putting user in "data" variable
cout << "again (Y/N)? "; //Outputting "again (Y/N)? "
cin >> pilih; //Putting user input into "pilih" variable
pushstack(data); //calling pushtack() function with argument "data"
} while(toupper(pilih) != 'N');//...until there will be character 'N' in "pilih" variable
cout << endl << endl << "Data you've filled:" << endl; //Outputting "\n\nData you've filled:\n"
while(top != NULL) //While "top" global variable does not equals to NULL...
cout << popstack() << endl;//...Outputting result of popstack() function
getch(); //Get character
return 0; //Return 0 to calling program
} //end of program
You will need two variables "front" (old "top") and "back". For "front" variable and pop() function everything can stay like it now, but push function will change slightly: you will push to back, so you will need to create new node; and make back-> next and, later, back point to it