Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views6 pages

Practice File (Lab 04)

Dsa lab 04

Uploaded by

Mudasar Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Practice File (Lab 04)

Dsa lab 04

Uploaded by

Mudasar Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Issue Date:October 13, 2024

Data Structures and Algorithms Lab


Practice File

Task 01: [20 Marks]


Circular Singly LinkedList implementation
In previous Lab you have implemented Singly Linear LinkedList. Modify implementation of that LinkedList

and make it circular Singly Linked List in such a way that next pointer of the last node points to the first(head)
node.
template<class T>

class LinkedList;
template<class T>

class Node
{
public:
T info;

Node<T>* next;

// Methods…
};

template<class T>
class LinkedList

Madiha Khalid

Department of Software Engineering, University of the

Punjab.

1
Issue Date:October 13, 2024
Data Structures and Algorithms Lab
Practice File

Node<T>* head;
// Methods…
};

Implement following functions for List class.


1. Constructor, destructor, Copy-constructor.
2. void insertAtHead( T value )

3. void insertAtTail( T value )


4. bool deleteAtHead( )
5. bool deleteAtTail( )
6. bool insertAfter( T value, T key )

Insert a node after some node whose info equals input parameter key and returns true if node is
successfully inserted, false otherwise.

7. bool insertBefore( T value, T key )


Insert a node before some node whose info equals input parameter key and returns true if node is

successfully inserted, false otherwise.


8. bool deleteBefore( T key )

Delete a node that is before some node whose info equals input parameter key and returns true if
node is successfully inserted, false otherwise. Check boundary cases i.e if node to be deleted is last

node or first node in the list.

Madiha Khalid

Department of Software Engineering, University of the

Punjab.

2
Issue Date:October 13, 2024
Data Structures and Algorithms Lab
Practice File

9. bool deleteAfter( T value )


Delete a node that is after some node whose info equals input parameter key and returns true if node
is successfully inserted, false otherwise. Check boundary cases i.e if node to be deleted is last node

or first node in the list.


10. int getLength( ) returns the total number of nodes in the list.
11. Node* search(T x)

Search a node with value “x” from list and return its link. If multiple nodes of same value exist, then
return pointer to first node having the value “x”.

Task 02: [20 Marks]


Doubly LinkedList implementation
Now implementADT of Doubly Linked list, In Doubly linked list we use 2 pointers (next and previous) next
pointing to the next node and previous to the the node behing it, prev of head node and next of last node is null.

template<class T>
class LinkedList;

template<class T>
class Node

Madiha Khalid

Department of Software Engineering, University of the

Punjab.

3
Issue Date:October 13, 2024
Data Structures and Algorithms Lab
Practice File

public:
T info;
Node<T>* next;

Node<T>* prev;
// Methods…
};

template<class T>
class LinkedList
{
Node<T>* head;

// Methods…
};

Implement following functions for List class.


1. Constructor, destructor, Copy-constructor.

2. void insertAtHead( T value )


3. void insertAtTail( T value )

4. bool deleteAtHead( )
5. bool deleteAtTail( )

6. bool insertAfter( T value, T key )

Madiha Khalid

Department of Software Engineering, University of the

Punjab.

4
Issue Date:October 13, 2024
Data Structures and Algorithms Lab
Practice File

Insert a node after some node whose info equals input parameter key and returns true if node is
successfully inserted, false otherwise.
7. bool insertBefore( T value, T key )

Insert a node before some node whose info equals input parameter key and returns true if node is
successfully inserted, false otherwise.
8. bool deleteBefore( T key )

Delete a node that is before some node whose info equals input parameter key and returns true if
node is successfully inserted, false otherwise. Check boundary cases i.e if node to be deleted is last
node or first node in the list.
9. bool deleteAfter( T value )

Delete a node that is after some node whose info equals input parameter key and returns true if node
is successfully inserted, false otherwise. Check boundary cases i.e if node to be deleted is last node

or first node in the list.


10. int getLength( ) returns the total number of nodes in the list.

11. Node* search(T x)


Search a node with value “x” from list and return its link. If multiple nodes of same value exist, then

return pointer to first node having the value “x”.

Task 03: [20 Marks]

Madiha Khalid

Department of Software Engineering, University of the

Punjab.

5
Issue Date:October 13, 2024
Data Structures and Algorithms Lab
Practice File

A) Merge Lists
Implement the following public member function of the Circular linked List:

Node* mergeLinkedLists(Node* head1, Node* head2) //returns the head of resulting merged list
This function will combine the nodes of the two linked lists (given their heads) into one list. All the nodes of
the first list will precede (come before) all the nodes of the second list.

B) Delete Evens
Implement the following public member function of the Circular linked List:
Bool deleteEvenNodes(Node* head)

Delete all the even number of nodes from a list whose head is given.

C) Sort List
Implement the following public member function of the Circular linked List:

Node* sortLinkedList(Node* head)


Sorts a linked list in ascending order.

Madiha Khalid

Department of Software Engineering, University of the

Punjab.

You might also like