Last active
April 8, 2017 11:18
-
-
Save israr-ahmad/213b629a6ce564dd65c21451359a3655 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <windows.h> | |
using namespace std; | |
//--------->>>AAHHOOO!!!!!!!!!!...................<<<------------------------------ | |
struct Node | |
{ | |
int data; | |
Node* next; | |
}; | |
bool isEmpty(Node* top); | |
int Push(Node* newNode,Node** top, int listSize); | |
int Pop(Node** top,int listSize); | |
void Print(Node* top); | |
int main() | |
{ | |
int choice ; // for user option | |
int listSize = 0; // How much array is filled ... data size | |
int value, key; | |
Node *top = NULL; | |
Node* newNode = NULL; | |
cout << "======================================================" <<endl; | |
cout << " \n Menu Driven Program & LIST ADT \n"; | |
cout << "======================================================" << endl << endl; | |
do | |
{ | |
cout << "|------------------------------------|" <<endl | |
<< "| 1- Insert a value |" <<endl | |
<< "| 2- Delete a value |" <<endl | |
<< "| 3- Print |" <<endl | |
<< "| 4- Exit |" <<endl | |
<< "|------------------------------------|" <<endl; | |
cout << "Enter your choice : "; | |
cin >> choice; | |
// function calls and required working will be added here | |
switch(choice) | |
{ | |
case 1: | |
cout << "Enter Value to be inserted: "; | |
cin >> value; | |
newNode = new Node; | |
newNode->data = value; | |
newNode->next = NULL; | |
listSize = Push(newNode,&top,listSize); | |
Print(top); | |
system("pause"); | |
break; | |
case 2: | |
if(isEmpty(top)) | |
{ | |
cout <<"stack is empty\n"; | |
} | |
else | |
{ | |
listSize = Pop(&top,listSize); | |
Print(top); | |
} | |
system("pause"); | |
break; | |
case 3: | |
if(isEmpty(top)) | |
{ | |
cout <<"stack is empty\n"; | |
} | |
else | |
{ | |
Print(top); | |
} | |
system("pause"); | |
break; | |
default: | |
exit(0); | |
break; | |
} | |
}while (choice != 4 ); | |
return 0; | |
} | |
bool isEmpty(Node* top) | |
{ | |
if(top == NULL) | |
return true; | |
else | |
return false; | |
} | |
int Push(Node* newNode,Node** top, int listSize) | |
{ | |
if(*top == NULL) | |
{ | |
*top = newNode; | |
newNode->next=NULL; | |
listSize++; | |
cout << "insert value special case:\n "; | |
return listSize; | |
} | |
else | |
{ | |
newNode->next=*top; | |
*top=newNode; | |
listSize++; | |
cout << "insert value first node case:\n "; | |
} | |
return listSize; | |
} | |
int Pop(Node** top,int listSize) | |
{ | |
Node* Current = *top; | |
*top=(*top)->next; | |
delete Current; | |
return listSize--; | |
cout << "delete value successfully:\n "; | |
} | |
void Print(Node* top) | |
{ | |
Node* Current = top; | |
int position = 1; | |
cout << "Value\t\tPosition" << endl; | |
while ( Current != NULL) | |
{ | |
cout << Current -> data <<"\t\t"<<position<<endl; | |
position++; | |
Current = Current ->next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Share and learn.
If you find any mistake so please tell me.