Dsa 7
Dsa 7
Dsa 7
BS(AI)3-A Semester
03(Fall 2022)
Exercise 1
Run the linked list implementation, discussed in the class. Call all the functions in the driver
program and check the output.
Exercise 2
Write a function which takes two values as input from the user and searches them in the list.
If both the values are found, your task is to swap both the nodes in which these values are
found.
Note, that you are not supposed to swap values. You are supposed to change next pointer fields
in the concerned nodes. Your function should handle all possible cases:
a. The two nodes are not adjacent and none of them is the first node.
b. The two nodes are not adjacent to each other and one of them is the first node.
c. Both nodes are adjacent and none of them is the first node.
d. Both nodes are adjacent and one of them is the first node
Exercise 3
Write a function which reverses the order of the linked list. If the original list is 1-> 2->3-
>4>5 -> 6, the updated list should be 6 -> 5 -> 4 -> 3 -> 2 ->1. Note that this function should
only change pointer fields in the nodes, you are not allowed to modify data field of any node
-------------------------------Class------------------------
#include <iostream>
#include <stdlib.h>
CSL-210: Data Structures and Algorithms Lab
BS(AI)3-A Semester
03(Fall 2022)
class NumberList
{ private
:
struct ListNode
{
double value;
struct ListNode *next;
};
ListNode *head;
public :
NumberList()
{
head = NULL;
}
//~NumberList();
nodePtr = head;
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//if the new node is to be the 1st in the list
//insert it before all other nodes
if (previousNode == NULL)
{
head = newNode;
newNode->next=nodePtr;
}
else //otherwise insert after the previous node
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
//deleting a specific node
void deleteNode(double num)
{
ListNode *nodePtr; //To transverse the List
ListNode *previousNode;//To point to the previous Node
//skip all the nodes whose value member is not equal to num
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
while(nodePtr!=NULL)
{
num = nodePtr->value;
nodePtr=nodePtr->next;
cout<<num<<endl;
}
}
//function to swap two nodes
void swap_nodes(double p,double q)
{
//struct ListNode *next = NULL;
//ListNode **head =NULL;
// struct node *prevNode1 = NULL, *prevNode2 = NULL, *node1 = head, *node2 = head;
CSL-210: Data Structures and Algorithms Lab
BS(AI)3-A Semester
03(Fall 2022)
if(p==q){
return;
} //if p and q are same, break
while(curNode != NULL) {
//3. While the curNode is not null adjust links
// (unlink curNode and link it to the reversed list
// from front and modify curNode and prevNode)
tempNode = curNode->next; curNode->next =
prevNode; prevNode = curNode;
curNode = tempNode;
}
};
CSL-210: Data Structures and Algorithms Lab
BS(AI)3-A
NumberList list;
list.appendNode(2.2);
list.insertNode(4.2);
list.insertNode(3.1);
list.insertNode(2.1);
list.insertNode(1.2);
list.displayList();
double num1,num2;
cout << "Enter Num1 : ";
cin >> num1; cout <<
"Enter Num2 : ";
cin >> num2;