using namespace std;
struct nodeWords
{
char words[100];
struct nodeWords* next;
};
struct undoWords
{
char unwords[100];
struct undoWords* next;
};
undoWords* untop;
undoWords* newundoNode;
nodeWords* top; //these pointers are used to hold the linked list.
nodeWords* current; //use for traversal
nodeWords* newNode; //use to create new node for new record
nodeWords* pop;
void Push() //push an element into the stack
{
char answer;
do
{
newNode = new nodeWords;
cout < "\nwrite="" what's="" in="" your="" mind.."=""><>
cout < "your="" words=""> ";
cin >> ws;
cin.getline(newNode->words, 100);
newNode->next = top;
top = newNode;
cout < "\nwant="" to="" write="" more?="" press="" y="" for="" yes="" and="" n="" for="" no:="">
cin >> answer;
} while (toupper(answer) == 'Y');
}
void stacktop() //display top of stack
{
if (top == NULL)
{
cout < "\nthere="" is="" no="">
}
else
{
cout < "\nthe="" top="" element="" is.....="">
cout < "\nyour="" words=""> " < top-="">words <>
}
}
void undo() //undo last pop
{
char answer2;
do {
if (untop == NULL)
{
cout < "\nthere="" is="" no="" words="" to="">
return;
}
else
{
newNode = new nodeWords;
newundoNode = new undoWords;
strcpy(newNode->words, untop->unwords);
newNode->next = top;
top = newNode;
untop = untop->next;
newundoNode->next = untop;
untop = newundoNode;
cout < "\n\nundo="" operation="">
cout < "\nyour="" words=""> " < top-="">words <>
untop = untop->next;
}
cout < "\npress="" y="" to="" go="" back="" to="" main="" menu:="">
cin >> answer2;
} while (toupper(answer2) != 'Y');
}
void display() //display all record
{
char answer2;
int no = 1;
int totalRecord = 0;
{
cout < endl="">< left="">< setw(5)="">< "no"="">< setw(50)="">< "your="" words"=""><>
cout < "----------------------------------------------------------------------"=""><>
current = top;
while (current != NULL)
{
cout < left="">< setw(5)="">< no="">< setw(50)="">< current-="">words <>
no++;
current = current->next;
totalRecord++;
}
cout < "----------------------------------------------------------------------"=""><>
cout < "\ntotal="" number="" of="" records:="" "="">< totalrecord=""><>
}
cout < "\npress="" y="" to="" go="" back="" to="" main="" menu:="">
cin >> answer2;
}
void Pop() //remove the last element added
{
char answer4;
{
current = top;
while (current != NULL)
{
pop = current;
if (top == NULL)
{
cout < "\nthere="" is="" no="" words="" to="">
return;
}
else(current == top);
{
newundoNode = new undoWords;
strcpy(newundoNode->unwords, top->words);
untop = newundoNode;
top = top->next;
current = NULL;
delete pop;
cout < "\n\npop="" operation="">
}
cout < "\npress="" y="" to="" go="" back="" to="" main="" menu:="">
cin >> answer4;
}
}
}
int main() //main menu
{
int menuOption;
top = NULL; //create list
do
{
cout <>
cout < "\t\tmain="">
cout <>
cout < "1="" -="" push/add="" record="" \n";="" add="">
cout < "2="" -="" peek/stack="" top="" \n";="" view="" the="" top="" of="" the="">
cout < "3="" -="" pop/remove="" record="" \n";="" delete="" the="" top="" of="" the="">
cout < "4="" -="" display\n";="" view="" all="">
cout < "5="" -="" undo\n";="">
cout < "6="" -="" exit\n";="" end="">
cout <>
cout < "enter="" your="" option="" [1,="" 2,="" 3,="" 4,="" 5,="" 6,="" 7]:="">
cin >> menuOption;
switch (menuOption)
{
case 1: Push();
break;
case 2: stacktop();
break;
case 3: current = top;
if (current == NULL)
{
cout < "\n\n="" the="" list="" is="">
cout < "unable="" to="" perform="" the="" delete="">
}
else {
Pop();
}
break; 5;
case 4: display();
break;
case 5: undo();
break;
case 6: cout < system("cls")="">< "\n\n\t="" program="">
break;
default: cout < "wrong="" option.="" please="" re-enter="" your="">
}
} while (menuOption != 6);
return 0;
}