You are on page 1of 7

NATIONAL UNIVERSITY OF SCIENCES

& TECHNOLOGY

DATA STRUCTURES & ALGORITHMS


LAB 04: IMPLEMENTATION OF LINKED LIST WITH
ITS OPERATIONS
BESE 12-B

Lab Engineer:
Anum Asif

Submitted by:
Muhammad Ashhub Ali
CMS ID: 380078
Lab Task
Write a C++ program that can
1. Create a simple linked list using functions to insert nodes at the head.
2. Make a function that can insert another node at 3rd location.
3. Make a function that can display the lists made in 1 and 2.
4. Write a function that can delete node from the linked list selected by the user. Display
it as well.
5. Write a function that can count the number of nodes present in list.
6. Create menu in main function to give call to all of the above functions depending
upon user’s input.

Important Note: Please note that you have to develop your solution in C++ (OOP) i.e. using
classes and objects. Solution written in a procedural style will not be accepted.
Code:

#include <iostream>

using namespace std;

class node //classifying node of linked list


{
public:
int value;
node* next;
};

node* head = NULL; //initialising head of linked list

void insertAtHead(int new_value)


{
node* newNode = new node; //creating new node
newNode->value = new_value;//adding values of node
newNode->next = NULL;//pointing this new node to NULL

//WRITE BELOW THE CODE FOR INSERTION AT HEAD


if (!head) {
head = newNode;
}
else {
newNode->next = head;
head = newNode;
}
}

void insertAtLocation(int location, int new_value)


{
//WRITE BELOW THE CODE FOR INSERTION AT LOCATION
node* newNode = new node;
newNode->value = new_value;
newNode->next = NULL;
node* nodePtr, * prevPtr;
if (!head || location == 0) {
newNode->next = head;
head = newNode;
}
else {
nodePtr = head->next;
prevPtr = head;
int count = 1;
while (count < location && !nodePtr) {
prevPtr = nodePtr;
nodePtr = nodePtr->next;
}
prevPtr->next = newNode;
newNode->next = nodePtr;
}
}

void del(int del_value)


{
//WRITE BELOW THE CODE FOR DELETION
node* nodePtr, * prevPtr;
nodePtr = head;
if (head->value == del_value) {
head = head->next;
delete nodePtr;
}
else {
while (nodePtr->next) {
prevPtr = nodePtr;
nodePtr = nodePtr->next;
if (nodePtr->value == del_value) {
prevPtr->next = nodePtr->next;
delete nodePtr;
break;
}
}
}
}

void displayList()
{
//WRITE BELOW THE CODE FOR DISPLAYING THE LIST
node* nodePtr = head;
while (nodePtr) {
if (nodePtr->next) {
cout << nodePtr->value << "->";
}
else {
cout << nodePtr->value << endl;
}
nodePtr = nodePtr->next;
}
}
void countList()
{
//WRITE BELOW THE CODE FOR COUNTING OF NODES
node* nodePtr = head;
int count = 0;
while (nodePtr) {
count++;
nodePtr = nodePtr->next;
}
cout << "Size: " << count << endl;
}

int main()
{
int cont = 1;
while (cont) {
char a;
cout << "\nSelect from one of the following:\nA. Create a node or Insert at HEAD\nB. Display
Linked list entries\nC. Insert at location\n";
cout << "D. Delete Value\nE. Count List\nF. End\n";
cin >> a;
switch (a) {
case 'A':
cout << "\nInsert any value to Create a node:" << endl;
int a1;
cin >> a1;
insertAtHead(a1);
break;
case 'B':
cout << "\nDisplay Linked list entries:" << endl;
displayList();
break;
case 'C':
cout << "\nChoose which location to enter the value:" << endl;
int b1, b2;
cin >> b1;
cout << "\nChoose the value to be entered:" << endl;
cin >> b2;
insertAtLocation(b1, b2);
break;
case 'D':
cout << "Choose value to be deleted:" << endl;
int d1;
cin >> d1;
del(d1);
break;
case 'E':
countList();
break;
case 'F':
cont = 0;
break;
default:
cout << "Invalid choice!" << endl;
}
}
}
Output:

You might also like