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

DS Lab Worksheet 1.3

The document contains programs to find the number of occurrences of an item in a linked list and to multiply each element of a linked list by 10. The first program inserts nodes into a linked list and finds the number of times a given value occurs. The second program displays the original linked list, multiplies each element by 10, and displays the new list.

Uploaded by

aditya pol
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)
77 views6 pages

DS Lab Worksheet 1.3

The document contains programs to find the number of occurrences of an item in a linked list and to multiply each element of a linked list by 10. The first program inserts nodes into a linked list and finds the number of times a given value occurs. The second program displays the original linked list, multiplies each element by 10, and displays the new list.

Uploaded by

aditya pol
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

Worksheet - 3

Name : Branch : CSE

UID : Subject : DS lab

Ques : 1

Write a program to print the total number of occurrences of a given item


in the linked list.

Program Code:
#include <bits/stdc++.h>
using namespace std;

class node
{
public:
int data;
node *next;
node(int val)
{
this->data = val;
next = NULL;
}
};

void insert_end(node *head, int val)


{
node *n = new node(val);
if (head == NULL)
{
head = n;
return;
}
node *temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = n;
}

void occurance(node *root, int val)


{
int ans = 0;
node *temp = root;
while (temp)
{
if (temp->data == val)
ans++;
temp = temp->next;
}
cout << "occurance of " << val << " is " << ans << " times." << endl;
}

int main()
{
int val;
cout << "Enter the number: ";
cin >> val;
node *head = new node(1);
insert_end(head, 2);
insert_end(head, 3);
insert_end(head, 4);
insert_end(head, 5);
insert_end(head, 4);
insert_end(head, 2);
insert_end(head, 1);
insert_end(head, 2);
insert_end(head, 3);
insert_end(head, 5);
insert_end(head, 6);
insert_end(head, 5);
insert_end(head, 6);
insert_end(head, 5);
insert_end(head, 5);
insert_end(head, 5);
insert_end(head, 4);
insert_end(head, 4);
insert_end(head, 5);
insert_end(head, 3);
insert_end(head, 6);
insert_end(head, 2);
occurance(head, val);
return 0;
}

Output:
Ques : 2

Write a program to multiply every element of the linked list with 10.

Program Code:
#include <iostream>
using namespace std;

class node
{
public:
int data;
node *next;
node(int val)
{
this->data = val;
next = NULL;
}
};

void insert_end(node *head, int val)


{
node *n = new node(val);
if (head == NULL)
{
head = n;
return;
}
node *temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = n;
}

void display(node *head)


{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL\n";
}

void multiply(node *&root)


{
node *temp = root;
while (temp)
{
temp->data *= 10;
temp = temp->next;
}
}

int main()
{
node *head = new node(1);
insert_end(head, 2);
insert_end(head, 3);
insert_end(head, 4);
insert_end(head, 5);
insert_end(head, 2);
insert_end(head, 3);
insert_end(head, 4);
insert_end(head, 5);
multiply(head);
display(head);
return 0;
}
Output:

You might also like