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

Name Anurag Yadav STUDENT ID 202212095 M.SC It SEM-1 LAB02 1.reverse Singly List

The document contains code solutions for 5 different data structures problems on linked lists: 1. A program to reverse a singly linked list by changing the next pointers. 2. A function to remove duplicate elements from a sorted linked list. 3. A function to count the number of nodes in a circular linked list by traversing it. 4. A function to delete a node from a doubly linked list given its position. 5. A function to find the middle element of a singly linked list by using two pointers, one moving twice as fast as the other.

Uploaded by

Anurag Yadav
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)
65 views

Name Anurag Yadav STUDENT ID 202212095 M.SC It SEM-1 LAB02 1.reverse Singly List

The document contains code solutions for 5 different data structures problems on linked lists: 1. A program to reverse a singly linked list by changing the next pointers. 2. A function to remove duplicate elements from a sorted linked list. 3. A function to count the number of nodes in a circular linked list by traversing it. 4. A function to delete a node from a doubly linked list given its position. 5. A function to find the middle element of a singly linked list by using two pointers, one moving twice as fast as the other.

Uploaded by

Anurag Yadav
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/ 11

NAME= ANURAG YADAV

STUDENT ID=202212095

M.Sc IT

SEM-1

LAB02

1.Reverse singly list


// dsa.cpp : This file contains the 'main' function. Program execution begins and ends there.

#include<iostream>
using namespace std;

struct node {
int data;
struct node* next;
};

void insert(struct node** head_ref, int data) {


struct node* node;
node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->next = (*head_ref);
(*head_ref) = node;
}

void reverse(struct node** head_ref) {


struct node* nxt = NULL;
struct node* prev = NULL;
struct node* current = (*head_ref);
while (current != NULL) {
nxt = current->next;
current->next = prev;
prev = current;
current = nxt;
}
(*head_ref) = prev;
}

void printnodes(struct node* head) {


while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
}

int main() {
struct node* head = NULL;
insert(&head, 1);
insert(&head, 3);
insert(&head, 2);
insert(&head, 5);
insert(&head, 4);
cout << "Linked List Before Reversing" << endl;
printnodes(head);
reverse(&head);
cout << endl;
cout << "Linked List After Reversing" << endl;
printnodes(head);
return 0;
}

2. Remove duplicated from sorted linked list


#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};

void removeDuplicates(Node* head)


{
Node* current = head;

Node* next_next;

if (current == NULL)
return;

while (current->next != NULL)


{
if (current->data == current->next->data)
{
next_next = current->next->next;
free(current->next);
current->next = next_next;
}
else
{
current = current->next;
}
}
}

void push(Node** head_ref, int new_data)


{

Node* new_node = new Node();

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;
}
void printList(Node* node)
{
while (node != NULL)
{
cout << " " << node->data;
node = node->next;
}
}

int main()
{
Node* head = NULL;
push(&head, 4);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 2);
push(&head, 1);

cout << "Linked list before duplicate removal "<<endl;


printList(head);

removeDuplicates(head);

cout << "\nLinked list after duplicate removal "<<endl;


printList(head);

return 0;
}

3. Count nodes in circular linked list

#include <iostream>
struct node {
int data;
struct node* next;
};
void push(struct node** head_ref, int data) {
struct node* ptr1 = (struct node*)malloc(sizeof(struct node));
struct node* temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;

if (*head_ref != NULL) {
while (temp->next != *head_ref) {
temp = temp->next;
}
temp->next = ptr1;
}
else {
ptr1->next = ptr1;
}
*head_ref = ptr1;
}

int count_fun(struct node* head) {


struct node* temp = head;
int result = 0;
if (head != NULL) {
do {
temp = temp->next;
result++;
} while (temp != head);
}
return result;
}
int main() {

struct node* head = NULL;


push(&head, 10);
push(&head, 20);
push(&head, 30);
push(&head, 40);
printf("count of nodes are: %d", count_fun(head));
return 0;
}
4.Delete element from doubly linked list (position wise)

include<iostream>
using namespace std;

struct Node {
int data;
struct Node* next;
struct Node* prev;
};

void deleteNode(struct Node** head_ref, struct Node* del)


{
if (*head_ref == NULL || del == NULL)
return;

if (*head_ref == del)
*head_ref = del->next;

if (del->next != NULL)
del->next->prev = del->prev;

if (del->prev != NULL)
del->prev->next = del->next;

free(del);
}

void deleteNodeAtGivenPos(struct Node** head_ref, int n)


{

if (*head_ref == NULL || n <= 0)


return;

struct Node* current = *head_ref;


int i;

for (int i = 1; current != NULL && i < n; i++)


current = current->next;

if (current == NULL)
return;

deleteNode(head_ref, current);
}

void push(struct Node** head_ref, int new_data)


{

struct Node* new_node =


(struct Node*)malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->prev = NULL;

new_node->next = (*head_ref);

if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;

(*head_ref) = new_node;
}

void printList(struct Node* head)


{
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
}
int main()
{

struct Node* head = NULL;

push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
cout << "Doubly linked list before deletion: ";
printList(head);
int n;
cout <<endl<< "enter n=";
cin >> n;
cout << endl;

deleteNodeAtGivenPos(&head, n);

cout << "Doubly linked list after deletion : ";


printList(head);

return 0;
}

5. Find the middle element of the singly linked list


#include <iostream>
using namespace std;

struct node
{
int num;
node* nextptr;
}*stnode;

void make(int n)
{
struct node* frntNode, * tmp;
int num, i;

stnode = (struct node*)malloc(sizeof(struct node));


if (stnode == NULL)
{
cout << "Memory can not be allocated";
}
else
{

cout << "Enter the data for node 1: ";


cin >> num;
stnode->num = num;
stnode->nextptr = NULL;
tmp = stnode;

for (i = 2; i <= n; i++)


{
frntNode = (struct node*)malloc(sizeof(struct node));

if (frntNode == NULL)
{
cout << "Memory can not be allocated";
break;
}
else
{
cout << "Enter the data for node= " << i << ": ";
cin >> num;
frntNode->num = num;
frntNode->nextptr = NULL;
tmp->nextptr = frntNode;
tmp = tmp->nextptr;
}
}
}
}
void print()
{
struct node* tmp;
if (stnode == NULL)
{
cout << "List is empty";
}
else
{
tmp = stnode;
cout << "Linked List=\t";
while (tmp != NULL)
{
cout << tmp->num << "\t";
tmp = tmp->nextptr;
}
}
}
void printMiddle(struct node* stnode)
{
struct node* single_ptr = stnode;
struct node* twice_ptr = stnode;

if (stnode != NULL)
{
while (twice_ptr != NULL && twice_ptr->nextptr != NULL)
{
twice_ptr = twice_ptr->nextptr->nextptr;
single_ptr = single_ptr->nextptr;
}
cout <<endl<< "The middle element is= " << single_ptr->num;
}
}
int main()
{
int n, num;

cout << "Enter the number of nodes= ";


cin >> n;
make(n);
cout << "\nLinked list data= \n";
print();
printMiddle(stnode);

return 0;
}

You might also like