Professional Documents
Culture Documents
Lab # 04
Submitted to:
Sir Tanveer Ali
Submitted by:
Isra Imran
Warda-tu-Zahra
BSEE (17-21)
Section A
Submission Date:
24-05-2021
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;
};
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);
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.
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;
//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;
if(current->next == NULL) {
return NULL;
} else {
//store reference to current link
previous = current;
return current;
}
if(current->next == NULL) {
return NULL;
} else {
//store reference to current link
previous = current;
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.