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

Lab3 - Linked List - Solution

The document contains instructions for three experiments on linked lists: 1) Implementing a linked list ADT with operations like insertion and deletion at different positions and reversing the list. 2) Demonstrating how a linked list can be used as a stack with push and pop operations. 3) Demonstrating how a linked list can be used as a queue with enqueue and dequeue operations.

Uploaded by

demro channel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Lab3 - Linked List - Solution

The document contains instructions for three experiments on linked lists: 1) Implementing a linked list ADT with operations like insertion and deletion at different positions and reversing the list. 2) Demonstrating how a linked list can be used as a stack with push and pop operations. 3) Demonstrating how a linked list can be used as a queue with enqueue and dequeue operations.

Uploaded by

demro channel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Benha University Electrical Engineering Department

Benha Faculty of Engineering Data Structures and Algorithms


(E1324)

lab (3) Linked List

LIST OF EXPERIMENTS
1. Linked List ADT implementation and perform insertion and deletion at end of
the list. Perform insertion and deletion at front of it. Perform insertion and deletion at
any position. And how reverse the list.
2. Demonstrate how this linked list can be used as STACK.
3. Demonstrate how this linked list can be used as QUEUE.
INTRODUCTION
Linked List is a linear data structure and it is very common data structure which consists
of group of nodes in a sequence which is divided in two parts. Each node consists of its
own data and the address of the next node and forms a chain. Linked Lists are used to
create trees and graphs. The principal benefit of a linked list over a conventional array
is that the list elements can easily be inserted or removed without reallocation or
reorganization of the entire structure because the data items need not be stored
contiguously in memory or on disk, while an array has to be declared in the source code,
before compiling and running the program. Linked lists allow insertion and removal of
nodes at any point in the list and can do so with a constant number of operations if the
link previous to the link being added or removed is maintained during list traversal. On
the other hand, simple linked lists by themselves do not allow random access to the
data, or any form of efficient indexing. Thus, many basic operations—such as obtaining
the last node of the list, or finding a node that contains a given datum, or locating the
place where a new node should be inserted— may require sequential scanning of most
or all of the list elements.
Types of Linked List:
Singly Linked List: Singly linked lists contain nodes which have a data part as well
as an address part i.e. next, which points to the next node in sequence of nodes. The
operations we can perform on singly linked lists are insertion, deletion and reverse.
Doubly Linked List: In a doubly linked list, each node contains two links the first link
points to the previous node and the next link points to the next node in the sequence.
Circular Linked List: In the circular linked list the last node of the list contains the
address of the first node and forms a circular chain
Experiment No: 1
Linked List ADT
Aim: To implement a Linked List ADT with operations add (), delete () etc.
Objectives:
1) Understand a list data structure and its basic operation.
2) Understand the method of defining list ADT and its basic operation.
3) Learn how to create objects from an ADT and member functions are invoked.

Algorithm:
#include <iostream>
#define null 0
using namespace std;
struct node
{
int data;
node *next;
};

typedef node *ptr;

void show(ptr head)


{
ptr temp=head;
while(temp!= null)
{
cout<< temp->data<<endl;
temp=temp->next;
}
}

void add_first(ptr &head, int val)


{
ptr p= new node;
p->data=val;
p-> next =head;
head=p;
}
void add_last(ptr &n, int val)
{
ptr p= new node;
p->data=val;
p-> next =null;
ptr temp=n;
while(temp-> next!= null)
{
temp=temp->next;
}
temp->next=p;
}
void add_anyposition(ptr &n, int val)
{
ptr p= new node;
p->data=val;
p-> next =n->next;
n->next=p;
}

void delete_first(ptr &head)


{
ptr p= head;
head =p->next;
delete(p);
}

void delete_anynode(ptr &n)


{
ptr p= n->next;
n->next =p->next;
delete(p);
}
void delete_last(ptr &head)
{
ptr p,n;
p=head;
while (p->next !=null)
{
n=p;
p=p->next;
}
n->next=null;
delete(p);

void reverse(ptr &head)


{
ptr prev= null;
ptr curr= head;
ptr next= null;

while(curr != null)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head= prev;
}

int main()
{
node *n1=new node;// another method for define node
ptr n2= new node;
ptr n3= new node;
ptr head = n1;
n1-> data=10;
n1-> next=n2;
n2-> data=20;
n2-> next=n3;
n3-> data=40;
n3-> next=null;
cout<<"before reverse"<<endl;
show(head);// total list

reverse(head);
cout<<"reverse list"<<endl;
show(head);

/*//add_first(head,300);
add_last(n1,50);
add_anyposition(n1, 200);
//cout<<head<<endl;// address of n1
show(head);// total list
cout<<"==============="<<endl;
delete_first(head);
show(head);// total list
cout<<"================"<<endl;
delete_anynode(n2);
show(head);// total list
cout<<"================"<<endl;

delete_last(head);
show(head);// total list*/

}
Program: To be written by the student using the above algorithm.
Observation: To be written by student after implementing the algorithm.
1)Input:
2)Output:
Experiment No: 2
Stack using Linked List ADT

Aim: To write C++ code to implement the Stack using Linked List ADT.
Objectives:
1) Understand the Stack data structure and its basic operation.
2) Understand the method of defining stack using Linked List ADT.
3) Learn how to create objects from an ADT and invoke member functions
Theory:
We have already seen how to represent stack using arrays. Such a representation proves
to be efficient if we have only one stack. However, when several stacks co-exit, there
is no efficient way to implement them sequentially. A good solution to this problem is
to use Linked List to implement stack. Such a stack is called Linked Stack. Linked
Stacks facilitates easy insertion and deletion of stack elements. In this experiment, we
use the head of the Linked List for insertion and deletion. A stack can be easily
implemented through the linked list. In stack Implementation, a stack contains a top pointer.
which is “head” of the stack where pushing and popping items happens at the head of the list.
first node has null in link field and second node link have first node address in link field and so
on and last node address in “top” pointer.The main advantage of using linked list over an array
is that it is possible to implements a stack that can shrink or grow as much as needed. In using
array will put a restriction to the maximum capacity of the array which can lead to stack
overflow. Here each new node will be dynamically allocated. so, overflow is not possible.
Algorithm:

#include <iostream>

#define null 0
using namespace std;

struct node
{
int data;
node *next;
};
node *top;
typedef node *ptr;

void push(int val)


{
ptr p=new node;
p->data=val;

if(top!=NULL){
p->next=top;
top=p;}
else{
p->next=NULL;
top=p;
}

void pop()
{
node *temp = top;
if(top!=NULL)
{
top=top->next;
delete temp;
}
else{
cout<<"stack is empty";
}
}
void display()
{
node *N=top;
if(top!=NULL)
{cout<< "Stack elements are: "<<endl;
while (N!=NULL)
{
cout<< N->data <<endl;
N = N->next;
}
}
else{
cout<<"stack is empty";
}
}

int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Program:To be written by the student using the above algorithm.
Observations:To be written by student after implementing the algorithm.
1)Input:
2)Output:
Experiment No: 3
Queue using Linked List ADT

Aim: To implement the Queue using Linked List ADT.

Objectives:
1) Understand the Queue data structure and its basic operation.
2) Understand the method of defining queue using Linked List ADT.
3) Learn how to create objects from an ADT and invoke member functions

Theory:
The major problem with the queue implemented using an array is, It will work for an
only fixed number of data values. That means, the amount of data must be specified at
the beginning itself. Queue using an array is not suitable when we don't know the size
of data which we are going to use. A queue data structure can be implemented using a
linked list data structure. The queue which is implemented using a linked list can work
for an unlimited number of values. That means, queue using linked list can work for
the variable size of data (No need to fix the size at the beginning of the implementation).
The Queue implemented using linked list can organize as many data values as we want.
In linked list implementation of a queue, the last inserted node is always pointed by
'rear' and the first node is always pointed by 'front'.

Algorithm:
#include <iostream>
#include <malloc.h>
using namespace std;
struct node
{
int data;
node *next;
};

typedef node *ptr;

ptr frront=NULL ;
ptr rear =NULL;

void enqueue()
{
int val;
cout<<"Insert the element in queue : "<<endl;
cin>>val;

ptr newnode= new node;


newnode->data=val;
newnode->next=NULL;

if ((frront == NULL) && (rear == NULL)) {


frront=rear=newnode;}
else{

rear->next=newnode;
rear=newnode;
}}
void dequeue()
{
ptr temp=frront;

if (frront == NULL) {
cout<<"Underflow"<<endl;
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
cout<<"Element deleted from queue is : "<<frront->data<<endl;
free(frront);
frront = temp;
} else {
cout<<"Element deleted from queue is : "<<frront->data<<endl;
free(frront);
frront = NULL;
rear = NULL;
}
}
void Display()
{
ptr tmp = new node;
tmp=frront;

if ((frront == NULL) && (rear == NULL)) {


cout<<"Queue is empty"<<endl;
return;
}
else{

cout<<"Queue elements are: "<<endl;


while (tmp != NULL)
{
cout<<tmp->data<<" "<<endl;
tmp = tmp->next;
}}
cout<<endl;
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}

Program: To be written by the student using the above algorithm.


Observations: To be written by student after implementing the algorithm.
1)Input:
2)Output:

You might also like