Professional Documents
Culture Documents
Linked List
1. Please modify the linked list program provided to produce the sample of
output is as below:
Sample of Output:
**Display list**
10 12 5 8 25
**Display list**
10 5 8 25
Reversed list
25 8 5 10
**Delete list**
**Display list**
Sorry the list is empty
// declare a head pointer as a global variable, so that it can be used in other functions
Nom *head;
}
/* This insert function will insert an element into the list.
*/
void insert(){
//create a new element that need to be inserted into the list
Nom *newptr, *cur, *prev;
cur = head;
prev = NULL;
void display(){
Nom *cur;
cur = head;
cout<<"\n**Display list**\n";
if (head == NULL){
cout<<"\nSorry the list is empty"<<endl;
}
else {
while(cur != NULL){
cout<<cur->iNom<<"->";
cur = cur->next;
}
cout<<"NULL\n";
}
}
/* Search function is used to search an element in the list.
* If the element is found, an appropriate msg will be displayed.
* Otherwise another appropriate msg will also be displayed.
*/
void search(){
int iData;
Nom *cur;
cout<<"\nPlease enter a data that you want to find : ";
cin>>iData;
cur = head;
//move cur pointer along the list
while(cur != NULL && iData != cur->iNom){
cur = cur->next;
}
if(cur == NULL)
cout<<"\nSorry the data is not found\n";
else
cout<<"\nYes, the data is found!\n";
}
/* Remove function is a function that removes only one node from the list.
* The function will prompt a user to enter a value to be removed and the
* date will be searched from the list. If it is found, then it will be removed,
* Otherwise, we simply display a message saying that the data is not found
* So, we cannot remove the data.
*/
void remove(){
int iData;
Nom *cur, *prev;