CAN ANYONE EXPLAIN ME!!

please help explain me what the meaning each line in this sricpt..??

#pragma hdrstop
#pragma argsused

#include <tchar.h>
#include <stdio.h>

#include <iostream.h>
#include <conio.h>
#include <string>


struct node;
typedef node *nodeptr;
struct node
{
string data;
nodeptr next;
};

nodeptr top;

void pushstack(string data)
{
nodeptr temp;
temp = new node;
temp->data = data;
temp->next = top;
top = temp;
}

string popstack()
{
if(top == NULL) return "";
nodeptr temp;
string data = top->data;
temp = top;
top = top->next;
delete temp;
return data;
}

int _tmain(int argc, _TCHAR* argv[])
{
top = NULL;
string data;
char pilih;
do
{
cout << "Input data > ";
cin >> data;
cout << "again (Y/N)? ";
cin >> pilih;
pushstack(data);
} while(toupper(pilih) != 'N');
cout << endl << endl << "Data you've filled:" << endl;
while(top != NULL)
cout << popstack() << endl;
getch();
return 0;
}
Useless comments team to the rescue!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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 
thank you
ehmm..@miniipaa if i want change this script to queue,which i must change?..
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
Topic archived. No new replies allowed.