0% found this document useful (1 vote)
126 views6 pages

Assignment 2 Struktur Data

This C++ program implements a linked list data structure with functions to insert, display, search, remove, and reverse nodes in the list. The main function creates an empty linked list and calls the various functions to demonstrate the linked list's capabilities, including inserting multiple nodes, displaying the list, removing nodes, searching for a value, reversing the order of nodes, and removing all nodes from the list. Key functions like insert(), display(), remove(), search(), reverse(), and remove_all() manipulate the global head pointer and pointers within each node to manage the linked list.

Uploaded by

Zaro Iqma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
126 views6 pages

Assignment 2 Struktur Data

This C++ program implements a linked list data structure with functions to insert, display, search, remove, and reverse nodes in the list. The main function creates an empty linked list and calls the various functions to demonstrate the linked list's capabilities, including inserting multiple nodes, displaying the list, removing nodes, searching for a value, reversing the order of nodes, and removing all nodes from the list. Key functions like insert(), display(), remove(), search(), reverse(), and remove_all() manipulate the global head pointer and pointers within each node to manage the linked list.

Uploaded by

Zaro Iqma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CODE

#include<iostream>
using namespace std;
void insert();
void display();
void remove_all();
void remove();
void search();
void reverse();
void printReverse();
void remove();

//create the structure of the list


struct Nom{
int iNom;
Nom *next;
Nom **head;
};

// declare a head pointer as a global variable, so that it can be used in other functions
Nom *head;

/* This is the main function.


*/
int main(){

//create an empty list


head = NULL ;
insert(); //insert the first element
insert(); //to insert another element
insert(); //to insert another element
insert(); //to insert another element
insert(); //to insert another element
display();//display all elements in the list
remove(); //call the remove function
display();//display all elements in the list
remove(); //call the remove function
search(); //call the search function
reverse();
printReverse();
remove_all(); //call the remove_all function
display();//display all elements in the list

/* Note that you can call insert function by using a loop


* example of inserting 10 elements:
* for (int i = 0; i< 10; i++) {
* insert();
*}
* You can also call display() function anywhere if you want
* to see your elements in the list
*/

}
/* 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;

newptr = new Nom;


cout<<"\nPlease enter a new value to be inserted : ";
cin>>newptr->iNom;
newptr->next = NULL;

//insert into an empty list


if(head == NULL){
head = newptr;
}
else{
// a loop to move prev and cur along the list and stop at appropriate place
// to insert a new element
while(cur != NULL && newptr->iNom){
prev = cur;
cur = cur->next;
}
//insert in front of the list
if(prev == NULL){
newptr->next = cur;
head = newptr;
}
else {
//coding to insert at the middle or the end of the list
newptr->next = cur;
prev->next = newptr;
}
}
}

/* This function will remove all nodes from the list.


* After calling this function, the list is empty
* Thus, no more node is available to view, update or remove.
*/
void remove_all(){
Nom *cur;
cur = head;
while(head != NULL){
cur = head; // cur points to the head
head = head->next; //head points to the next
delete cur;
}
}

/* Display function is a function to display all elements in the list.


* This function will dislay "sorry the list is empty", if the list is empty.
* Otherwise it will display all elements in the list
*/
void display(){
Nom *cur;
cur = head;
if (head == NULL){
cout<<"\nSorry the list is empty"<<endl;
}
else {
while(cur != NULL){
cout<<cur->iNom<<" ";
cur = cur->next;
}

}
}

/* Function to reverse the linked list */

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<<"";
}
}

/* 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;

if(head == NULL){ //we cannot remove if the list is empty


cout<<"\nSorry, the list is empty"<<endl;
}
else{
cout<<"\nPlease enter a data to be removed : ";
cin>>iData;
prev = NULL;
cur = head;
/*move prev and cur pointers along the list */
while(cur != NULL && iData != cur->iNom){
prev = cur;
cur = cur->next;
}
if(prev == NULL){
head = head->next;
delete cur;
}
else {
if(cur == NULL){
cout<<"\nSorry we cannot remove the data as it is not in the list"<<endl;
}
else{
prev->next = cur->next;
delete cur;
}
}
}
}
OUTPUT

You might also like