Assignment 2 Struktur Data
Assignment 2 Struktur Data
#include<iostream>
using namespace std;
void insert();
void display();
void remove_all();
void remove();
void search();
void reverse();
void printReverse();
void remove();
// 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 reverse(){
Nom *cur=head;
Nom*prev=NULL,*next=NULL;
while(cur!=NULL)
{
next=cur->next;
cur->next=prev;
prev=cur;
cur=next;
}
head=prev;
}
void printReverse(){
Nom *cur;
cur=head;
cout<<"\nReverse List\n";
if(head==NULL)
{
cout<<"\nSorry the list is empty"<<endl;
}
else{
while(cur!=NULL)
{
cout<<cur-> iNom<<" ";
cur=cur->next;
}
cout<<"";
}
}
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;