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

Lab 5 Imran Shaheer 19F-0285 Problem # 1

The document contains solutions to 11 problems related to linked lists in C++. Problem 1 defines a node structure and functions to insert nodes, check for a node, delete a node, and display the list. Problem 2 defines a linked list class with functions to insert nodes, remove duplicate values, and display the list. The remaining problems implement various linked list operations like reversing a list, creating a circular list, and sorting a list using insertion sort.

Uploaded by

Imran Shaheer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Lab 5 Imran Shaheer 19F-0285 Problem # 1

The document contains solutions to 11 problems related to linked lists in C++. Problem 1 defines a node structure and functions to insert nodes, check for a node, delete a node, and display the list. Problem 2 defines a linked list class with functions to insert nodes, remove duplicate values, and display the list. The remaining problems implement various linked list operations like reversing a list, creating a circular list, and sorting a list using insertion sort.

Uploaded by

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

Lab 5

Imran Shaheer
19F-0285
Problem # 1:
#include<iostream>
using namespace std;

struct node
{
int data;
struct node* prev;
struct node* next;
};
struct node;
node* head = NULL;
node* tail = NULL;
node* check_node(int d)
{

node* temp = NULL;


node* ptr = head;

while (ptr != NULL)


{
if (ptr->data == d)
{

temp = ptr;
}
ptr = ptr->next;
}
return temp;

}
void insert(node* new_node)
{

if (head == NULL)
{
head = tail = new_node;
head->prev = NULL;
tail->next = NULL;
}
else
{
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
tail->next = NULL;
}

bool Is_Empty()
{
if (head == NULL)
{
return true;
}
}
void DeleteNode(int d)
{

node* ptr = check_node(d);


if (ptr == NULL)
{
cout << "there is no node" << endl;
}
else
{
if (head->data == d)
{
head = head->next;
cout << "unlinked" << endl;
}
else
{
node* nextnode = ptr->next;
node* prevnode = ptr->prev;
if (nextnode == NULL)
{
prevnode->next = nextnode;
cout << "The Node deleted" << endl;
}
else
{
prevnode->next = nextnode;
nextnode->prev = prevnode;
cout << "The Node deleted" << endl;
}
}
}
}
void display()
{
node* current = head;
if (head == NULL)
{
cout << "The list is empty:" << endl;
return;
}
while (current != NULL)
{
cout << current->data << "->";
current = current->next;
}
cout << "NULL";
cout << endl;
}

int main()
{

int i = 0;
while (i < 4) {
cout << "Enter data:";
node* new_node = new node;
cin >> new_node->data;
insert(new_node);
i++;
}
cout << "The list before deleted" << endl;
display();
DeleteNode(3);
display();
system("pause");
return 0;
}

Problem # 2:
Code:
#include<iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
class linklist {
struct Node* head = NULL;
struct Node* tail = NULL;
public:
void insert(Node*new_node);
void duplicate();
void display();
};
void linklist::insert(Node*new_node) {

if (head == NULL)
{
head = tail = new_node;
head->prev = NULL;
tail->next = NULL;
}
else
{
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
tail->next = NULL;
}

}
void linklist::duplicate() {
Node *current;
Node *tmp;
Node *tmp1;
if (head == NULL)
{
cout << "The list is empty:" << endl;
}
else
{
for (current = head; current != NULL; current = current->next)
{
for (tmp = current->next; tmp != NULL; tmp = tmp->next)
{
if (current->data == tmp->data)
{
tmp1 = tmp;
tmp->prev->next = tmp->next;
if (tmp->next != NULL)
tmp->next->prev = tmp->prev;
tmp1 = NULL;
}
}
}
}
cout << "The list after deleting duplicate value:" << endl;
}
void linklist::display()
{
Node* current = head;
if (head == NULL)
{
cout << "The list is empty:" << endl;
return;
}
while (current != NULL)
{
cout << current->data << "->";
current = current->next;
}
cout << "NULL";
cout << endl;
}
int main()
{
linklist obj1;
int i = 0;
while (i < 4) {
cout << "Enter data:";
Node* new_node = new Node;
cin>>new_node->data;
obj1.insert(new_node);
i++;
}
cout << "The list before duplicating: " << endl;
obj1.display();
obj1.duplicate();
obj1.display();
system("pause");
return 0;
}

Problem # 10:
Code:
#include<iostream>
using namespace std;

struct node
{
int data;
struct node* prev;
struct node* next;
};
class linklist {
struct node* head = NULL;
struct node* tail = NULL;
public:
void insert(node* new_node);
void display();
void reverse();
};

void linklist::insert(node*new_node) {

if (head == NULL)
{
head = tail = new_node;
head->prev = NULL;
tail->next = NULL;
}
else
{
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
tail->next = NULL;
}

}
void linklist::display()
{
node* current = head;
if (head == NULL)
{
cout << "The list is empty:" << endl;
return;
}
while (current != NULL)
{
cout << current->data << "->";
current = current->next;
}
cout << "NULL";
cout << endl;
}
void linklist:: reverse()
{
node* tmp1 = NULL;
node* tmp2 = head;

while (tmp2 != NULL)


{
tmp1 = tmp2->prev;
tmp2->prev = tmp2->next;
tmp2->next = tmp1;
tmp2 = tmp2->prev;
}

if (tmp1 != NULL)
{
head = tmp1->prev;
}
}
int main()
{

linklist obj1;
int i = 0;
while (i < 4) {
cout << "Enter data:";
node* new_node = new node;
cin>>new_node->data;
obj1.insert(new_node);
i++;
}
cout << "List is before reverse: " << endl;
obj1.display();
cout << "List after reverse: " << endl;
obj1.reverse();
obj1.display();
system("pause");
return 0;
}

Problem # 11:
Code
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node* prev;
struct Node* next;
};
struct Node;
Node* head = NULL;
Node* tail = NULL;
void insert(int data)
{
Node* new_Node = new Node();
new_Node->data = data;

if (head == NULL)
{
head = tail = new_Node;
head->prev = NULL;
tail->next = NULL;
}
else
{

tail->prev = new_Node;
new_Node->next = tail;
tail = new_Node;
}
}

void display()
{
Node* current = head;
if (head == NULL)
{
cout << "LIST IS EMPTY " << endl;
return;
}
while (current != NULL)
{
cout << current->data << "->";
current = current->prev;
}
cout << "NULL";
cout << endl;
}
int main()
{
insert(1);
insert(2);
insert(3);
insert(4);
cout << "circular link list is:" << endl;
display();
system("pause");
return 0;
}

Problem # 6:
Code:
#include<iostream>
using namespace std;

struct node
{
int data;
struct node* prev;
struct node* next;
};
struct node;
node* head = NULL;
node* tail = NULL;

void insert(int data)


{
node* new_node = new node();
new_node->data = data;

if (head == NULL)
{
head = tail = new_node;
head->prev = NULL;
tail->next = NULL;
}
else
{

tail->next = new_node;
new_node->prev = tail;
tail = new_node;
tail->next = NULL;
}
{
int tmp1;
int cnt;
node* temp2 = new node;
node* ptr = NULL;
do
{
temp2 = head;
cnt = 0;
while (temp2->next != ptr)
{
if (temp2->data > temp2->next->data)
{
tmp1 = temp2->data;
temp2->data = temp2->next->data;
temp2->next->data = tmp1;
cnt = 1;
}
temp2 = temp2->next;
}
ptr = temp2;
} while (cnt);
}
}

void display()
{
node* current = head;
if (head == NULL)
{
cout << "LIST IS EMPTY " << endl;
return;
}
while (current != NULL)
{
cout << current->data << "->";
current = current->next;
}

cout << "NULL";


cout << endl;
}

int main()
{

insert(1);
insert(2);
insert(3);
insert(4);
insert(5);

cout << "The list is " << endl;


display();
system("pause");
return 0;
}

You might also like