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

DSA Lab

The document discusses different data structures like arrays, linked lists and doubly linked lists in C++. It includes code snippets to implement counter arrays, matrix multiplication, singly linked lists with functions to insert, delete and traverse nodes, and doubly linked lists with functions to append, traverse forward and backward.
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)
9 views

DSA Lab

The document discusses different data structures like arrays, linked lists and doubly linked lists in C++. It includes code snippets to implement counter arrays, matrix multiplication, singly linked lists with functions to insert, delete and traverse nodes, and doubly linked lists with functions to append, traverse forward and backward.
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

DSA Lab

Counter array:
#include<iostream>
using namespace std;
int main () {
int a[3][3];
int counter = 0;
for (int i=0; i<3; i++){
for(int j=0; j<3; j++) {
counter = counter+1;
a[i][j] = counter;
}
}

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


for(int j=0; j<3; j++) {
cout<<a[i][j]<<" ";
}
}cout<<endl;
return 0;
}
Multiplication of an array:
#include<iostream>
using namespace std;
int main () {
int a[3][3];
int b[3][3];
int mul[3][3];

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


for(int j=0; j<=2; j++) {
cout<<"Enter elements in the matrix 1 at index "<<i<<"
"<<j<<endl; cin>>a[i][j];

}
}
for (int i=0; i<=2; i++){
for(int j=0; j<=2; j++) {
cout<<"Enter elements in the matrix 2 at index "<<i<<"
"<<j<<endl; cin>>b[i][j];

}
}
for (int i=0; i<=2; i++){
for(int j=0; j<=2; j++) {
mul[i][j] = a[i][j] * b[i][j];
cout<<mul[i][j]<<" ";
}
}
return 0;
}
Linked list:
#include<iostream>
#include<string>
using namespace std;

struct node{
string data;
node *next;
node(string value):data(value),next(NULL){ }
};
class linkedlist {
private:
node* head;
public:
linkedlist():head(NULL){
}
void append(string value){
node* newnode = new node(value);
if(!head){
head=newnode;
}
else{
node* current = head;
while(current->next){
current = current->next;
}
current->next=newnode;
}
}
void display(){
node*current=head;
while (current!=NULL){
cout<<current->data<<"->";
current = current->next;
}
cout<<"\nNULL";
}

void insertatend(string value) {


node* newnode = new node(value);
newnode->data = value;
newnode->next = NULL;
if(head == NULL) {
head = newnode;
} else {
node* temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
}

void insertatposition(string value, int position){


node*newnode = new node(value);
if (position==0) {
newnode->next=head;
head=newnode;
return;
}
node* current = head;
for (int i= 0; i<position - 1 && current; ++i) { current
= current->next;

}
if(!current){
return;
}
newnode->next = current->next;
current->next = newnode;
}
void deletenode(string value) {
node* current = head;
node* prev = NULL;

while (current && current->data != value){ prev =


current;

current = current->next;
}
if (current) {
if(prev) {
prev->next = current->next;
}
else {
head = current->next;
}
delete current;
}
}
};

int main () {
linkedlist list;
list.append("muntaha");
list.append("rida");
list.append("laraib");
cout<<"Linked List: "<<endl;
list.display();
cout<<endl;

list.insertatend("aila");
cout<<"Linked list after inserting at the end: "<<endl;
list.display();

cout<<endl;

list.insertatposition("kashaf",3);
cout<<"Linked list after inserting at the position 2: "<<endl;
list.display();

cout<<endl;
list.deletenode("kashaf");
cout<<"Linked list after deleting node with value 2: "<<endl;
list.display();
cout<<endl;

return 0;
}

Output:
Linked list:
#include<iostream>
using namespace std;

struct node{
int data;
node *next;
node(int value):data(value),next(NULL){ }
};
class linkedlist {
private:
node* head;
public:
linkedlist():head(NULL){
}
void append(int value){
node* newnode = new node(value);
if(!head){
head=newnode;
}
else{
node* current = head;
while(current->next){
current = current->next;
}
current->next=newnode;
}
}
void display(){
node*current=head;
while (current!=NULL){
cout<<current->data<<"->";
current = current->next;
}
cout<<"\nNULL";
}

void insertatbeginning (int value) {


node* newnode = new node(value);
newnode->next = head;
head = newnode;
}

void insertatposition(int value, int position){


node*newnode = new node(value);

if (position==0) {
newnode->next=head;
head=newnode;
return;
}
node* current = head;
for (int i= 0; i<position - 1 && current; ++i) { current
= current->next;

}
if(!current){
return;
}
newnode->next = current->next;
current->next = newnode;
}

void deletenode(int value) {


node* current = head;
node* prev = NULL;

while (current && current->data != value){


prev = current;
current = current->next;
}
if (current) {
if(prev) {
prev->next = current->next;
}
else {
head = current->next;
}
delete current;
}
}
};

int main () {
linkedlist l;
l.append(1);
l.append(2);
l.append(3);
cout<<"Linked List: "<<endl;
l.display();
cout<<endl;

l.insertatbeginning(0);
cout<<"Linked list after inserting at the beginning: "<<endl;
l.display();
cout<<endl;

l.insertatposition(5,2);
cout<<"Linked list after inserting at the position 2: "<<endl;
l.display();

cout<<endl;

l.deletenode(2);
cout<<"Linked list after deleting node with value 2: "<<endl;
l.display();

cout<<endl;

return 0;
}
Output 1:
Double linked list:
#include<iostream>
using namespace std;

class node {
public:
int data;
node* prev;
node* next;

node (int value) {


data = value;
prev = NULL;
next = NULL;
}
};

class doublylinkedlist {
private:
node* head;
node* tail;
public:
doublylinkedlist () {
head = NULL;
tail = NULL;
}
void append (int value) {
node* newnode = new node(value); if (head
== NULL) {

head = newnode;
tail = newnode;
}
else {
tail -> next = newnode;
newnode -> prev = tail;
tail = newnode;
}
}

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

void displayreverse () {
node* current = tail;
while (current != NULL) {
cout<<current -> data<<" ";
current = current -> prev;
}
cout<<endl;
}
};

int main (){


doublylinkedlist dll;
dll.append(2);
dll.append(4);
dll.append(6);

cout<<"Forward: ";
dll.display();

cout<<"Reverse: ";
dll.displayreverse();

return 0;
}

Output:

Stack:
#include<iostream>
using namespace std;
const int MAX_SIZE = 100;

class stack {
private:
int arr[MAX_SIZE];
int top;

public:
stack () {
top = -1;
}

bool isEmpty() {
return top == -1;
}

bool isFull() {
return top == MAX_SIZE - 1;
}

void push (int value) {


if (isFull()) {
cout<<"Stack Overflow!"<<endl;
return;
}
arr[++top] = value;
}

int pop() {
if(isEmpty()) {
cout<<"Stack Underflow!" <<endl;
return -1;
}
return arr[top--];
}

int peek() {
if(isEmpty()) {
cout<<"Stack is Empty!" <<endl;
return -1;
}
return arr[top];
}
};

int main() {
stack st;
st.push(2);
st.push(4);
st.push(6);

cout<<"Top Element: "<<st.peek() <<endl;


cout<<"Popping Elements: ";
while (!st.isEmpty()) {
cout<< st.pop()<<" ";
}
cout<<endl;
return 0;
}

Output:
Stack with double linked list:
#include<iostream>
using namespace std;

class node {
public:
int data;
node* next;

node(int value) {
data = value;
next = NULL;
}
};

class stack {
private:
node* top;

public:
stack() {
top = NULL;
}

bool isEmpty() {
return top == NULL;
}

void push(int value) {


node* newnode= new node(value);
newnode -> next = top;
top = newnode;
}

int pop() {
if (isEmpty()) {
cout<<"Stack Underflow!"<<endl;
return -1;
}
node* temp = top;
int poppedvalue = temp -> data;
top = top->next;
delete temp;
return poppedvalue;

int peek() {
if (isEmpty()){
cout<<"Stack is empty!"<<endl;
return -1;
}
return top->data;
}
};

int main () {
stack st;
st.push(1);
st.push(3);
st.push(5);
cout<<"Top Element: "<<st.peek() <<endl;

cout<<"Popping Elements: ";


while (!st.isEmpty()) {
cout<< st.pop()<<" ";
}
cout<<endl;
return 0;
}

Output:

Queue using array:


#include<iostream>
#define MAX_SIZE 10
using namespace std;
int item[MAX_SIZE],rear,front;
init(){
rear = -1;
front = 0;
}
isEmpty(){
if(front>rear){
return true;
}else{
return false;
}
}
enqueue(int data){
item[++rear] = data;
}
dequeue(){
return item[front++];
}
display(){
if(!isEmpty()){
for(int i=front; i<=rear; i++)
cout<<item[i]<<endl;
}else{
cout<<"Queue is Empty"<<endl;
}
}
size(){
return (rear - front + 1);
}
isFull(){
if(size()>=MAX_SIZE){
return true;
}else{
return false;
}
}
int main(){
int choice, data;
init();
while(1){
cout<<"\n1. Enqueue\n2. Dequeue\n3. Size\n4. Display all element\n5. Quit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice){
case 1:
if(!isFull()){
cout<<"\nEnter data: ";
cin>>data;
enqueue(data);
}else{
cout<<"Queue is Full"<<endl;
}
break;
case 2:
if(!isEmpty()){
cout<<"The data dequeued is :"<<dequeue();
}else{
cout<<"Queue is Emtpy"<<endl;
}
break;
case 3:
cout<<"Size of Queue is "<<size();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
}
}
return 0;
}
Output:

You might also like