0% found this document useful (0 votes)
43 views

Exercise Source Code:: Atif Jalal 02-235191-027 BS (IT) - 3A Lab 08 Date: 14 July, 2020

The document contains C++ source code for a linked list-based program that allows the user to insert nodes at the end of the list, delete nodes from the end of the list, search for a node by value, display all nodes, and check if the list is empty. The code defines struct nodes with integer data and pointers, and functions for each operation on the linked list.

Uploaded by

Atif Jalal
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 (0 votes)
43 views

Exercise Source Code:: Atif Jalal 02-235191-027 BS (IT) - 3A Lab 08 Date: 14 July, 2020

The document contains C++ source code for a linked list-based program that allows the user to insert nodes at the end of the list, delete nodes from the end of the list, search for a node by value, display all nodes, and check if the list is empty. The code defines struct nodes with integer data and pointers, and functions for each operation on the linked list.

Uploaded by

Atif Jalal
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/ 4

Atif Jalal

02-235191-027
BS (IT)-3A Lab 08 Date: 14 July, 2020

EXERCISE

SOURCE CODE:
#include <iostream>
using namespace std;

struct node
{
int num;
node *next;
};

node *head = NULL;

void insertAtEnd(int n);


void deleteFromEnd();
void display();
bool isListEmpty();
int search(int);

void main()
{
int a = 0, b;
cout << "\n ---------------- LINKED LIST BASED LIST ---------------" << endl;
cout << " 1. Insert at end \n 2. Delete from end \n 3. Search " << endl;
cout << " 4. Display \n 5. Is List Empty? \n 6. Exit" << endl;
cout << " -------------------------------------------------------" << endl;
while (a != 6)
{
cout << "\n Enter your choice : ";
cin >> a;
switch (a)
Atif Jalal
02-235191-027
BS (IT)-3A Lab 08 Date: 14 July, 2020
{
case 1:
{
cout << " Enter number : ";
cin >> b;
insertAtEnd(b);
break;
}
case 2:
{
deleteFromEnd();
break;
}
case 3:
{
cout << " Enter number : ";
cin >> b;
if (search(b) == -1)
cout << "\n Value doesn't exist." << endl;
else
{
cout << "\n Value exists in node " << search(b) + 1 << "."
<< endl;
}
break;
}
case 4:
{
display();
break;
}
case 5:
{
if (isListEmpty())
{
cout << "\n List is Empty" << endl;
}
else
{
cout << "\n List is not Empty" << endl;
}
break;
}
case 6:
break;
default:
cout << "\n Select from the given (1-6) options." << endl;
}
}
system("pause");
}

void insertAtEnd(int n)
{
node *obj = new node;
obj->num = n;
obj->next = NULL;
if (head == NULL)
{
head = obj;
}
else
Atif Jalal
02-235191-027
BS (IT)-3A Lab 08 Date: 14 July, 2020
{
node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = obj;
}
}

void deleteFromEnd()
{
if (head == NULL)
{
cout << "\n List Empty";
}
else if (head->next == NULL)
{
delete head;
}
else
{
node *temp = head;
while (temp->next->next != NULL)
{
temp = temp->next;
}
delete (temp->next);
temp->next = NULL;

}
}

void display()
{
int a = 0;
if (head == NULL)
{
cout << "\n List Empty";
}
else
{
node *temp = head;
while (temp->next != NULL)
{
cout << " Num " <<a+1 <<" : " <<temp->num <<endl;
temp = temp->next;
a++;
}
cout << " Num " << a + 1 << " : " << temp->num <<endl;
}
}

bool isListEmpty()
{
if (head == NULL)
{
return true;
}
else
return false;
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 08 Date: 14 July, 2020

int search(int n)
{
int a = 0;
if (head == NULL)
{
cout << "\n List Empty";
}
else
{
node *temp = head;
while (temp->next != NULL)
{
if (n == temp->num)
{
return a;
}
else
{
temp = temp->next;
a++;
}
}
if (temp->num == n)
{
return a;
}
else
{
return a = -1;
}
}
}

OUTPUT:

You might also like