Last active
April 8, 2017 11:32
-
-
Save israr-ahmad/ca50ff693bceb0dcd37881e5932413d6 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; | |
struct Node | |
{ | |
int data; | |
Node* next; | |
}; | |
bool isEmpty(Node* Head); | |
int InsertAt(Node** Head, Node** Tail, Node* newNode, int listSize, int position); | |
int DeleteAt(Node** Head, Node** Tail, int listSize, int position); | |
int Search(Node* Head, int key); | |
int ClearList(Node** Head, Node** Tail, int listSize); | |
void PrintList(Node* Head); | |
void SortList(Node** Head, Node** Tail); | |
void ReverseList(Node** Head, Node** Tail); | |
int main() | |
{ | |
int choice ; // for user option | |
int listSize = 0; // How much list is filled ... data size | |
int position, value, key; | |
Node *Head = NULL, *Tail = 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- Search a value |" <<endl | |
<< "| 4- Print list |" <<endl | |
<< "| 5- Clear list |" <<endl | |
<< "| 6- Sort list |" <<endl | |
<< "| 7- Reverse list |" <<endl | |
<< "| 8- 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; | |
cout << "Enter Position for value: "; | |
cin >> position; | |
// if position is invalid then node must not be created | |
// discussed algo part for position validity comes here | |
if (position < 1 || position > listSize+1) | |
{ | |
cout << "Invalid position !!! \n Value cannot be inserted!!!\n"; | |
} | |
else | |
{ | |
newNode = new Node; | |
newNode->data = value; | |
newNode->next = NULL; | |
listSize = InsertAt(&Head, &Tail, newNode, listSize, position); | |
} | |
system("pause"); | |
break; | |
case 2: | |
if(isEmpty(Head)) | |
{ | |
cout <<"List is empty\n"; | |
} | |
else | |
{ | |
cout << "Enter Position for value: "; | |
cin >> position; | |
if (position < 1 || position > listSize) | |
{ | |
cout << "Invalid position !!! \n Value cannot be deleted!!!\n"; | |
} | |
else | |
{ | |
listSize = DeleteAt(&Head, &Tail, listSize, position); | |
} | |
} | |
system("pause"); | |
break; | |
case 3: | |
if(isEmpty(Head)) | |
{ | |
cout <<"List is empty\n"; | |
} | |
else | |
{ | |
cout << "Enter value for search: "; | |
cin >> key; | |
if (key < 1 ) | |
{ | |
cout << "Invalid value !!! \n Value cannot be searched!!!\n"; | |
} | |
else | |
{ | |
Search(Head, key); | |
} | |
} | |
system("pause"); | |
break; | |
case 4: | |
if(isEmpty(Head)) | |
{ | |
cout <<"List is empty\n"; | |
} | |
else | |
{ | |
PrintList(Head); | |
} | |
system("pause"); | |
break; | |
case 5: | |
if(isEmpty(Head)) | |
{ | |
cout <<"List is empty\n"; | |
} | |
else | |
{ | |
listSize=ClearList(&Head,&Tail,listSize); | |
} | |
system("pause"); | |
break; | |
case 6: | |
if(isEmpty(Head)) | |
{ | |
cout <<"List is empty\n"; | |
} | |
else | |
{ | |
SortList(&Head,&Tail); | |
} | |
system("pause"); | |
break; | |
case 7: | |
if(isEmpty(Head)) | |
{ | |
cout <<"List is empty\n"; | |
} | |
else | |
{ | |
ReverseList(&Head,&Tail); | |
} | |
cout <<"Now List is reverse\n"; | |
PrintList(Head); | |
system("pause"); | |
break; | |
default: | |
exit(0); | |
break; | |
} | |
}while (choice != 8 ); | |
return 0; | |
} | |
bool isEmpty(Node* Head) | |
{ | |
if(Head == NULL) | |
return true; | |
else | |
return false; | |
} | |
int InsertAt(Node** Head, Node** Tail, Node* newNode, int listSize, int position) | |
{ | |
Node *tempHead=*Head; | |
Node *tempTail=*Tail; | |
if(*Head == NULL) // List is empty | |
{ | |
*Head = newNode; | |
newNode->next=NULL; | |
*Tail = *Head; | |
listSize++; | |
cout <<"List is empty case\n"; | |
return listSize; | |
} | |
else if(position==1)//----------------->> Start | |
{ | |
newNode->next=*Head; | |
*Head=newNode; | |
listSize++; | |
cout <<"List is start case\n"; | |
return listSize; | |
} | |
else if(position==listSize+1)//---------------------->>> end | |
{ | |
tempTail->next=newNode; | |
newNode->next=NULL; | |
*Tail=newNode; | |
listSize++; | |
cout <<"List is end case\n"; | |
return listSize; | |
} | |
else //------------------------------------>> mid | |
{ | |
Node *prev=*Head; | |
Node *current=prev->next; | |
int counter=1; | |
while(current!=NULL) | |
{ | |
if(counter==position-1) | |
{ | |
newNode->next=current; | |
prev->next=newNode; | |
} | |
prev=current; | |
current=current->next; | |
counter++; | |
} | |
listSize++; | |
cout <<"List is mid case\n"; | |
return listSize; | |
} | |
} | |
int DeleteAt(Node** Head, Node** Tail, int listSize, int position) | |
{ | |
Node* Current = *Head; | |
Node* prev = *Head; | |
int nodeCount = 1; | |
if (Current->next ==NULL) //Special case one node | |
{ | |
*Head = NULL; | |
*Tail = NULL; | |
delete Current; | |
cout <<"delete first node special case\n"; | |
listSize--; | |
return listSize; | |
} | |
else if(position==1) //---------------------->>delete at Start | |
{ | |
Node* prev = *Head; | |
Node* current = prev->next; | |
{ | |
delete prev; | |
*Head=current; | |
cout <<"delete first case node existing others\n"; | |
} | |
listSize--; | |
return listSize; | |
} | |
else if(position==listSize)//==============>>delete at end | |
{ | |
Node* prev = *Head; | |
Node* current = prev->next; | |
while(current!=*Tail) | |
{ | |
prev=current; | |
current=current->next; | |
} | |
prev->next=NULL; | |
delete current; | |
*Tail=prev; | |
listSize--; | |
cout <<"delete at end successful\n"; | |
return listSize; | |
} | |
else//-------------------------->>delete at mid | |
{ | |
Node* prev = *Head; | |
Node* current = prev->next; | |
int nodeCount = 2; | |
while(current->next!=NULL) | |
{ | |
if(position==nodeCount) | |
{ | |
prev->next=current->next; | |
delete current; | |
} | |
prev=current; | |
current=current->next; | |
nodeCount++; | |
} | |
listSize--; | |
cout <<"delete at mid successful\n"; | |
return listSize; | |
} | |
} | |
int Search(Node* Head, int key)// ---------------->>problem last value not found | |
{ | |
Node *current = Head; | |
int nodeCount = 1; | |
while(current->next!=NULL) | |
{ | |
if(current->data==key) | |
{ | |
cout << "Value\t\tPosition" << endl; | |
cout << current -> data <<"\t\t"<<nodeCount<<endl; | |
} | |
current=current->next; | |
nodeCount++; | |
} | |
return key; | |
} | |
int ClearList(Node** Head, Node** Tail, int listSize) //------------->>problem | |
{ | |
cout <<"AHHOOO!!!....\n"; | |
Node* Current = *Head; | |
while( Current != NULL) | |
{ | |
Node* ptr = Current; | |
Current = Current ->next; | |
delete ptr; | |
listSize--; | |
} | |
*Head = NULL; | |
*Tail = NULL; | |
return listSize; | |
} | |
void PrintList(Node *Head) | |
{ | |
Node* Current = Head; | |
int position = 1; | |
cout << "Value\t\tPosition" << endl; | |
while ( Current != NULL) | |
{ | |
cout << Current -> data <<"\t\t"<<position<<endl; | |
position++; | |
Current = Current ->next; | |
} | |
} | |
void SortList(Node** Head, Node** Tail) | |
{ | |
cout <<"AHHOO run\n"; | |
Node* prev = *Head; | |
Node* current = *Head; | |
Node* temp = NULL; | |
while(current!=NULL) | |
{ | |
Node* prev1 = *Head; | |
while(prev1!=NULL) | |
{ | |
if(prev->data > current->data) | |
{ | |
swap(current->data, prev->data ); | |
cout <<"if part sort run\n"; | |
} | |
prev=prev->next; | |
current=prev; | |
} | |
current=current->next; | |
cout <<"sort run\n"; | |
} | |
} | |
void ReverseList(Node** Head, Node** Tail) | |
{ | |
Node* Current = *Head; | |
Node* prev = NULL; | |
Node* next = NULL; | |
while(Current!=NULL) | |
{ | |
next=Current->next; | |
Current->next=prev; | |
prev=Current; | |
Current=next; | |
} | |
*Head=prev; | |
} | |
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.