You are on page 1of 9

Data Structure and Algorithm (DSA)

Lab # 04

Submitted to:
Sir Tanveer Ali

Submitted by:
Isra Imran
Warda-tu-Zahra
BSEE (17-21)
Section A
Submission Date:
24-05-2021

Pakistan Institute of Engineering and Applied


Sciences (PIEAS)
Introduction to Data Structure List and
its implementation using Linked Lists
Objectives:
 Understanding of the data structure list
 Implementation of List using Singly and Doubly Linked List
 Implementing the basic operations of Linked Lists

Background:
A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers.  A linked list consists of nodes
where each node contains a data field and a reference to the next node in the list.

A singly linked list has only pointer to next element of the list. We cannot move to the previous
element directly. To overcome this difficulty, doubly linked list is used. It contains pointers to both
previous and next elements. In this lab, we have demonstrated the use of doubly linked list and
performed various operations on it. Also various operations on singly linked list are also
performed.

Lab 04(a):

Task 1
Create a class linkedlist and implement the following functions

1. InsertNodeAtFront()
2. InsertNodeAtBack()
3. DeleteNodeFromFront()
4. PrintList()

Task 2
Create Two Lists in the main Program as
L1: 5, 15, 25, 35
L2: 10, 20, 30, 40
Create L1 by adding values from front and L2 by adding values from back.
Create L3 in which you remove values from front from L1 and L2. Add the deleted values
from L1 and L2 in L3. e.g L3 with data 5, 10, 15, 20, 25, 30, 35, 40. Also, show the
memory addresses of the list values.

Code:
/*****SUBMITTED BY : ISRA IMRAN
WARDA TU ZAHRA********//////

#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
};
void InsertNodeAtFront(Node** head, int newdata ){
Node* new_node = new Node();
new_node->data = newdata;
new_node->next = (*head);
(*head)=new_node;
};
void InsertNodeAtBack( Node**head, int newdata ){
Node* new_node = new Node();
new_node->data = newdata;
new_node->next = NULL;
if (*head==NULL)
{
*head=new_node;
}
else{
Node *last_node = *head;
while (last_node->next !=NULL)
{
last_node = last_node->next;
}
last_node->next = new_node;
};
};
int DeleteNodeFromFront( Node** head) {
int a;
if (head==NULL)
{
cout<<" NO LIST CREATED";
}
Node *temp=*head;
*head=temp->next;
a=temp->data;
delete (temp);
return a;
};

void printList(Node *node)


{
while (node != NULL)
{
cout<<"List elements: "<<node->data<<" at address " << &node->data<< endl;
node = node->next;
}
};
int main(){
Node *head = NULL , *second = NULL,*third=NULL;
int a1,a2,a3,a4;
int b1,b2,b3,b4;

InsertNodeAtFront(&head,5);
InsertNodeAtFront(&head,15);
InsertNodeAtFront(&head,25);
InsertNodeAtFront(&head,35);
cout<<"Created Linked list is: "<< endl;
printList(head);
InsertNodeAtBack(&second,10);
InsertNodeAtBack(&second,20);
InsertNodeAtBack(&second,30);
InsertNodeAtBack(&second,40);

cout<<"Created Linked list 2 is: "<< endl;


printList(second);
cout<<"Created Linked list 3 is: "<< endl;

a1=DeleteNodeFromFront(&head);
InsertNodeAtFront(&third,a1);
b1=DeleteNodeFromFront(&second);
InsertNodeAtBack(&third,b1);
a2=DeleteNodeFromFront(&head);
InsertNodeAtBack(&third,a2);
b2=DeleteNodeFromFront(&second);
InsertNodeAtBack(&third,b2);
a3=DeleteNodeFromFront(&head);
InsertNodeAtBack(&third,a3);
b3=DeleteNodeFromFront(&second);
InsertNodeAtBack(&third,b3);
a4=DeleteNodeFromFront(&head);
InsertNodeAtBack(&third,a4);
b4=DeleteNodeFromFront(&second);
InsertNodeAtBack(&third,b4);
printList(third);
return 0;
}

Output:

Lab 04(b):
Create a doubly linked list and store data of at least 4 students: A student must have
two fields name and GPA.

1. Write a function that searches a student by its name,


2. Write a function that deletes a student from the list whose name is entered by
the user,
3. Show the memory addresses of the list values.

Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
float data;
string name;
Node *next;
Node *prev;
};
Node *head = NULL;
Node* last = NULL;
Node* current = NULL;

//is list empty


bool isEmpty() {
return head == NULL;
}
void printList(Node *node)
{
while (node != NULL)
{
cout<<" name ---------- "<<node->name;
cout<<" GPA ------------"<<node->data;
cout<<" at location ------------ "<<node<<endl;
cout<<" next node address "<<node->next;
cout<<" | previous node address "<<node->prev<<endl;
node = node->next;
}
}
//insert link at the first location
void insertFirst(string name, float data) {

//create a link
Node* new_node = new Node();
new_node->data = data;
new_node->name = name;

if(isEmpty()) {
//make it the last link
last =new_node ;
} else {
//update first prev link
head->prev = new_node;
}
//point it to old first link
new_node->next = head;

//point first to new first link


head = new_node;
}
// to find a student by name
Node* navigate(string name) {
//start from the first link
Node* current = head;
Node* previous = NULL;
//if list is empty
if(head == NULL) {
return NULL;
}

//navigate through list


while(current->name != name) {
//if it is last node

if(current->next == NULL) {
return NULL;
} else {
//store reference to current link
previous = current;

//move to next link


current = current->next;
}
}

return current;
}

Node* del(string name) {

//start from the first link


Node* current = head;
Node* previous = NULL;

//if list is empty


if(head == NULL) {
return NULL;
}

//navigate through list


while(current->name != name) {
//if it is last node

if(current->next == NULL) {
return NULL;
} else {
//store reference to current link
previous = current;

//move to next link


current = current->next;
}
}

//found a match, update the link


if(current == head) {
//change first to point to next link
head = head->next;
} else {
//bypass the current link
current->prev->next = current->next;
}

if(current == last) {
//change last to point to prev link
last = current->prev;
} else {
current->next->prev = current->prev;
}

return head;
}

int main() {
int choose;
string n;
cout<<"enter 1 to search a student,2 to delete it";
cin>>choose;
Node* locate=NULL;
insertFirst("ali",3.4);
insertFirst("warda",3.5);
insertFirst("rizvi",2.1);
insertFirst("zahra",1.9);
cout<<"initial list\n";
printList(head);
if(choose==1){
cout<<"enter name to search";
cin>>n;
locate=navigate(n);
cout<<locate->name<<" has GpA "<<locate->data<<" is at location "<<locate<<endl;
}
if(choose==2){
cout<<"enter name to search";
cin>>n;
del(n);
cout<<"after deleting, new list is\n";
printList(head);
}

return 0;
}

Output:

Conclusion:
In this lab, we have demonstrated use of singly and doubly linked list. First, we performed operations
like insertion, deletion of node on singly linked list. Then we performed these operations on doubly
linked list. In doubly linked list, we have the advantage to move both forward and backwards but it
requires more effort in insertion and deletion operations. Also, more space is required as two pointers
are stored in each node.

You might also like