Index
S.NO. TITLE SIGNATURE
1 WAP to create a * diamond pattern using 2d array
2 WAP to implement sparse matrix addition
WAP to implement sparse matrix transpose and
3
multiplication
4 WAP to implement linear single linklist.
5 WAP to implement linear doubly linklist.
6 WAP to implement circular linklist.
7 WAP to implement Stacks
8 WAP to implement Queues
i
4. Program to implement singly Linked list
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class SinglyLinkedList {
public:
Node* head;
SinglyLinkedList() : head(nullptr) {}
void insert(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
void deleteFirst() {
if (head) {
Node* temp = head;
head = head->next;
delete temp;
ii
void display() {
Node* temp = head;
while (temp) {
cout << temp->data << " -> ";
temp = temp->next;
cout << "nullptr" << endl;
};
int main() {
SinglyLinkedList list;
// Inserting elements
list.insert(10);
list.insert(20);
list.insert(30);
cout << "Singly Linked List after insertion: ";
list.display();
// Deleting the first element
list.deleteFirst();
cout << "Singly Linked List after deleting the first element: ";
list.display();
return 0;
Output:
iii
iv
5. Program to implement doubly linked list
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() : head(nullptr) {}
void insert(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
newNode->prev = nullptr;
if (head) head->prev = newNode;
head = newNode;
void deleteFirst() {
if (head) {
Node* temp = head;
head = head->next;
v
if (head) head->prev = nullptr;
delete temp;
void display() {
Node* temp = head;
while (temp) {
cout << temp->data << " <-> ";
temp = temp->next;
cout << "nullptr" << endl;
};
int main() {
DoublyLinkedList list;
// Inserting elements
list.insert(10);
list.insert(20);
list.insert(30);
cout << "Doubly Linked List after insertion: ";
list.display();
// Deleting the first element
list.deleteFirst();
cout << "Doubly Linked List after deleting the first element: ";
vi
list.display();
return 0;
Output:
vii
6. Program to implement circular Linked list
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class CircularLinkedList {
public:
Node* head;
CircularLinkedList() : head(nullptr) {}
void insert(int value) {
Node* newNode = new Node();
newNode->data = value;
if (!head) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
temp->next = newNode;
newNode->next = head;
viii
}
void display() {
if (!head) return;
Node* temp = head;
do {
cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
cout << "(head)" << endl;
};
int main() {
CircularLinkedList list;
// Inserting elements
list.insert(10);
list.insert(20);
list.insert(30);
cout << "Circular Linked List: ";
list.display();
return 0;
Output:
ix
7. Implement Stack using singly Linked list
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class Stack {
public:
Node* top;
Stack() : top(nullptr) {}
void push(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = top;
top = newNode;
void pop() {
if (top) {
Node* temp = top;
top = top->next;
delete temp;
x
void display() {
Node* temp = top;
while (temp) {
cout << temp->data << " -> ";
temp = temp->next;
cout << "nullptr" << endl;
};
int main() {
Stack stack;
// Pushing elements onto the stack
stack.push(10);
stack.push(20);
stack.push(30);
cout << "Stack after pushing elements: ";
stack.display();
// Popping an element from the stack
stack.pop();
cout << "Stack after popping an element: ";
stack.display();
return 0;
Output:
xi
xii
8. Implement Queue using linked list
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class Queue {
public:
Node *front, *rear;
Queue() : front(nullptr), rear(nullptr) {}
void enqueue(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (!rear) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
void dequeue() {
if (front) {
xiii
Node* temp = front;
front = front->next;
if (!front) rear = nullptr;
delete temp;
void display() {
Node* temp = front;
while (temp) {
cout << temp->data << " -> ";
temp = temp->next;
cout << "nullptr" << endl;
};
int main() {
Queue queue;
// Enqueueing elements
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
cout << "Queue after enqueueing elements: ";
queue.display();
// Dequeueing an element
xiv
queue.dequeue();
cout << "Queue after dequeueing an element: ";
queue.display();
return 0;
Output:
xv