Question: #include using namespace std; struct LIST { int Info; struct LIST *Next; }; struct LIST *HEAD = NULL; struct LIST *PREV, *CURR; void AddNode(int ITEM)
#include
struct LIST *PREV, *CURR;
void AddNode(int ITEM) { struct LIST *NewNode = new LIST; NewNode->Info=ITEM; NewNode->Next=NULL; if(HEAD == NULL) {HEAD = NewNode; return; } if(NewNode->Info < HEAD->Info) { NewNode->Next = HEAD; HEAD = NewNode; return; } PREV = CURR = NULL; for(CURR = HEAD ; CURR != NULL ; CURR = CURR ->Next) { if( NewNode->Info < CURR ->Info) break; else PREV = CURR; }
NewNode->Next = PREV->Next; PREV->Next = NewNode; }
void DeleteNode() { int inf; if(HEAD == NULL){ cout<< " empty linked list "; return;} cout<< " Put the info to delete: "; cin>>inf; if(inf == HEAD->Info) { HEAD = HEAD->Next; return;} PREV = CURR = NULL; for(CURR = HEAD ; CURR != NULL ; CURR = CURR ->Next ) { if(CURR ->Info == inf) break; PREV = CURR; } if( CURR == NULL) cout<
void Traverse() { for(CURR = HEAD; CURR != NULL ; CURR = CURR ->Next ) cout<< CURR ->Info<<"\t"; }
int search(int key) { int index; struct node *CURR;
index = 0; CURR = *HEAD;
while (CURR != NULL && CURR->data != key) { index++; CURR = CURR->next; }
return (CURR != NULL) ? index : -1; }
int main() { int inf, ch; while(1) { cout<< "Linked List Operations "; cout<< " 1- Add Node 2- Delete Node "; cout<< " 3- Traverse List 4- exit "; cout<< " Your Choice: "; cin>>ch; switch(ch) { case 1: cout<< " Put info/value to Add: "; cin>>inf; AddNode(inf); break; case 2: DeleteNode(); break; case 3: cout<< " Linked List Values: "; Traverse(); break; case 4: exit(0); } } return 0; }
complete code with linked list search function
C++ language
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
