[go: up one dir, main page]

0% found this document useful (0 votes)
12 views22 pages

Riya Assignment

Uploaded by

Nishita Karda
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)
12 views22 pages

Riya Assignment

Uploaded by

Nishita Karda
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/ 22

Name: Riya Soni

Enrollment no: 0801EI221081

DSOS LAB ASSIGNMENT


Que 1. Write a program to multiply and add two arrays .

#include <iostream> using

namespace std;

void addArrays(int arr1[], int arr2[], int result[], int size)

{ for (int i = 0; i < size; i++) { result[i] = arr1[i] +

arr2[i];

void multiplyArrays(int arr1[], int arr2[], int result[], int size)

{ for (int i = 0; i < size; i++) { result[i] = arr1[i] * arr2[i];

void displayArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

int main() {

int size;

cout << "Enter the size of the arrays: ";

cin >> size;

int arr1[size], arr2[size], result[size];


Name: Riya Soni
Enrollment no: 0801EI221081

cout << "Enter elements of the first array:

"; for (int i = 0; i < size; i++) { cin >>

arr1[i];

cout << "Enter elements of the second array: ";

for (int i = 0; i < size; i++) {

cin >> arr2[i];

// Adding arrays

addArrays(arr1, arr2, result, size);

cout << "Result of addition: ";

displayArray(result, size);

// Multiplying arrays

multiplyArrays(arr1, arr2, result, size);

cout << "Result of multiplication: ";

displayArray(result, size);

return 0;

}
Name: Riya Soni
Enrollment no: 0801EI221081

Ques.2) Write programs to implement stack by following ways:

1) Using array

2) Using Linked List

Implement Stack using Array

#include<iostream>

using namespace std;

class stack{ int *arr;

int top; int size;

public: stack(int n=100) {

size = n; arr = new int[size];

top = -1; } void

push(int num) {

if(top >= size - 1) { cout <<

"stack overflow" << endl;


Name: Riya Soni
Enrollment no: 0801EI221081

} else {

top++; arr[top]

= num;

}} int pop() {

if(top >= 0) { int ans

= arr[top]; top--;

return ans;

} else { cout << "stack

underflow" << endl; return -1;}

}
int peek() {

if(top >= 0) {

return arr[top];

} else { cout << "stack is

empty" << endl; return -1;

bool isEmpty() {

return (top == -1);

};

int main() {

stack s;

s.push(5);

s.push(10);

s.push(20);

s.push(30); cout<<"Pop element is"<<endl;

cout<<s.pop()<<endl; cout<<"Elements

present in Stack are:"<<endl;

while(!s.isEmpty()) { //Print elements in Stack

cout << s.peek() << endl;


Name: Riya Soni
Enrollment no: 0801EI221081

s.pop();

}return 0;

}
Implement Stack using Linked List
#include<iostream>
using namespace std;
class Node { public:
int data;
Node* next;
Node(int n) { this-
>data = n; this-
>next = NULL;
} };
class stack {
Node* head;
public: stack() {
head = NULL;
}

void push(int num) {

Node* temp = new Node(num);


temp->next = head; head =
temp;
}
bool isEmpty() {

return head == NULL;

}
Name: Riya Soni
Enrollment no: 0801EI221081

void pop() { if

(head == NULL) {

cout << "Stack underflow" << endl;

} else {

Node* temp = head;

head = head->next;

delete temp;

int peek() { if (head

!= NULL) return

head->data;

else return -1; // Returning -1 to indicate the

stack is empty

void display() { Node* temp

= head; while (temp !=

NULL) { cout << temp-

>data << " "; temp =

temp->next;

cout << endl;

};

int main() {

stack s;

s.push(10);

s.push(20);

s.push(30);

s.push(50);

s.pop();
Name: Riya Soni
Enrollment no: 0801EI221081

cout << "Top element is: " << s.peek() << endl;

s.pop();

cout << "Stack elements: ";

s.display();

return 0;

}
Name: Riya Soni
Enrollment no: 0801EI221081

Ques 3) Write a program for Insertion in singly Linked List

 Insertion at beginning

 Insertion at end

 Insertion at specific position

#include< iostream> using

namespace std;

class Node{

public:

int data;

Node*next;

Node(int value)

data = value;

next = nullptr;

};

Node* insertion_at_begin(Node*head,int value){ //Insertion at head

Node* newNode= new Node(value); newNode->next=head;

head=newNode;

return head;

Node* insert_at_end(Node*head,int value){ // Insertion at end

Node*newNode= new Node(value);

Node*temp=head; while(temp-

>next!=NULL){ temp=temp->next;
Name: Riya Soni
Enrollment no: 0801EI221081

}
temp->next=newNode; newNode-

>next=NULL;

return head;
}

Node*insert_at_key(Node*head,int key,int value){ //Insertion at any index

Node*newNode=new Node(value);

Node*temp=head;

if(key==1){ newNode-

>next=head; return

newNode;

for(int i=2;i<key;i++){ if(temp-

>next!=NULL){ temp=temp->next;

newNode->next=temp->next;

temp->next=newNode; return

head;

void printList(Node* node) //Print elements in linked list

while (node != nullptr) {

cout << node->data << " ";

node = node->next;

cout << endl;


}
Name: Riya Soni
Enrollment no: 0801EI221081

int main() {

Node* head = new Node(1);


head->next = new Node(2); head->next-

>next = new Node(3); head->next->next-

>next = new Node(6);

cout << "Original Linked List: ";

printList(head);

head=insertion_at_begin(head,5);

cout<<"Insertion at head:"<<endl;

printList(head);

head=insert_at_end(head,10);

cout<<"Insertion at tail:"<<endl;

printList(head);

head=insert_at_key(head,3,10);

cout<<"Insertion at key:"<<endl;

printList(head);

return 0;

}
Name: Riya Soni
Enrollment no: 0801EI221081

Ques 4) Write a program for Deletion in singly Linked List

 Deletion at beginning

 Deletion at end

 Deletion at specific position

#include< iostream> using


namespace std;

class Node{
public: int
data;
Node*next;
Node(int value){
data=value;
next=nullptr;
}
};

Node* delete_at_begin(Node*head){ //Delete node from beginning


if(head==NULL) return NULL;
Name: Riya Soni
Enrollment no: 0801EI221081

Node*temp=head;
head=head->next;
delete(temp);
return head;
}

Node*delete_at_end(Node*head){ //Delete node from tail


if(head==NULL || head->next==NULL){
delete head;
return NULL;
}
Node*temp=head; while(temp->next->next!=NULL){
temp=temp->next;

}
Node*lastNode=temp->next;
temp->next=NULL;
delete(lastNode); return
head;
}

Node*delete_at_key(Node*head,int key){ //Delete node from key


index if(key<=0 || head==NULL){ return head;
}
if(key==1){

return delete_at_begin(head);
}

Node*temp=head;
for(int i=1;i<key;i++){ if(temp-
>next!=NULL){ temp=temp->next;
}
else{
cout<<"Position out of Linked List"<<endl;
return head;
}
Name: Riya Soni
Enrollment no: 0801EI221081

Node*toDelete=temp->next;
temp->next=temp->next->next;
delete toDelete; return head;
}

void printList(Node* node) // Print all the elements


{

while (node != NULL) {


cout << node->data << " ";
node = node->next;
}
cout << endl;

}
int main() {

Node* head = new Node(1); head->next =


new Node(2); head->next->next = new
Node(3); head->next->next->next = new
Node(6); head->next->next->next-
>next=new Node(8); cout<<"Original Linked
List:"; printList(head);
head=delete_at_begin(head);
cout<<"Element delete at begin"<<endl;
printList(head); head=delete_at_key(head,3);
cout<<"Element delete at key
index"<<endl; printList(head);
head=delete_at_end(head);
cout<<"Element delete at end"<<endl;
printList(head); return 0;
}
Name: Riya Soni
Enrollment no: 0801EI221081

Ques. 5) Write a Program to Search Element in Linked List

#include< iostream>

using namespace std;

class node{ public: int

data;

node* next; node(int

val){

data = val;

next = NULL;

};

void element(node* head, int key) {

if (head == NULL) {

return;

node* temp = head; int count = 0; while (temp != NULL) { if (temp-

>data == key) { //check if element is present in linked list or not cout

<< "Element found at index: " << count << endl;

return;

temp = temp->next;

count++;

}
Name: Riya Soni
Enrollment no: 0801EI221081

cout << "Element not found" << endl;

void printList(node* head) {

while (head != NULL) { cout <<

head->data << " "; head =

head->next;

cout << endl;

int main() { node* head = new node(2);

head->next = new node(3); head->next->next

= new node(5); head->next->next->next =

new node(6); head->next->next->next->next

= new node(8);

int key = 5;

element(head, key);

return 0;

Ques.6) Write programs to implement queue by following ways:

1Using array
2Using Linked List
Name: Riya Soni
Enrollment no: 0801EI221081

Queue using array


#include <iostream> using
namespace std;

class Queue {
private: int front,
rear, size; int* arr;

public: Queue(int
capacity) { size =
capacity; arr = new
int[size]; front = 0;
rear = -1;
}

~Queue() { delete[]
arr;
}

bool isFull() { return


(rear == size - 1);
}

bool isEmpty() { return


(front > rear);
}

void enqueue(int data) {

if (isFull()) {
cout << "Queue is full. Cannot enqueue " << data << endl; return;
}

arr[++rear] = data;
}
Name: Riya Soni
Enrollment no: 0801EI221081

int dequeue() { if
(isEmpty()) {
cout << "Queue is empty. Cannot dequeue." << endl;
return -1; // or throw an exception
}
return arr[front++];

int peek() { if
(isEmpty()) {
cout << "Queue is empty. Cannot peek." << endl;
return -1; // or throw an exception
}

return arr[front];
}

void display() { if (isEmpty()) { cout


<< "Queue is empty." << endl;
return;
}
cout << "Queue elements: ";

for (int i = front; i <= rear; i++) { cout


<< arr[i] << " ";

cout << endl;


}

};

int main() {
Queue q(5); // Create a queue of capacity 5
Name: Riya Soni
Enrollment no: 0801EI221081

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);
q.enqueue(40);

q.enqueue(50);

q.display(); // Display the queue elements

cout << "Dequeued: " << q.dequeue() << endl;

q.display();

q.enqueue(60); // Trying to enqueue when queue is full

cout << "Front element is: " << q.peek() << endl;

return 0;

Queue using linked list


#include <iostream> using

namespace std;

struct Node { int

data;

Node* next;

};
Name: Riya Soni
Enrollment no: 0801EI221081

class Queue { private:

Node* front;

Node* rear;

public:

Queue() { front

= nullptr; rear

= nullptr;

~Queue() { while

(!isEmpty()) {

dequeue();

bool isEmpty() { return

front == nullptr;

void enqueue(int data) {

Node* newNode = new Node();

newNode->data = data; newNode-

>next = nullptr;

if (isEmpty()) {

front = rear = newNode;

} else {

rear->next = newNode; rear

= newNode;
Name: Riya Soni
Enrollment no: 0801EI221081

int dequeue() { if (isEmpty()) { cout << "Queue is

empty. Cannot dequeue." << endl; return -1; // or

throw an exception

Node* temp = front; int

data = front->data;

front = front->next;

if (front == nullptr) { rear

= nullptr;

delete temp; return

data;

int peek() {

if (isEmpty()) {

cout << "Queue is empty. Cannot peek." << endl;

return -1; // or throw an exception

return front->data;

void display() { if (isEmpty()) { cout

<< "Queue is empty." << endl;

return;
Name: Riya Soni
Enrollment no: 0801EI221081

Node* temp = front; cout

<< "Queue elements: ";

while (temp != nullptr) {

cout << temp->data << " ";

temp = temp->next;

cout << endl;

};

int main() {

Queue q;

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);

q.enqueue(40);

q.enqueue(50);

q.display(); // Display the queue elements

cout << "Dequeued: " << q.dequeue() << endl;

q.display();

cout << "Front element is: " << q.peek() << endl;

return 0;

}
Name: Riya Soni
Enrollment no: 0801EI221081

You might also like