Skip to content

Commit c4503c3

Browse files
Added C++ Data Structures
1 parent 77c24c4 commit c4503c3

File tree

5 files changed

+472
-0
lines changed

5 files changed

+472
-0
lines changed

Cpp/LinkedList.cpp

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// Implementation of a Linked List in C++
2+
#include <iostream>
3+
using namespace std;
4+
5+
// Implementation of a Node
6+
class Node{
7+
public:
8+
Node *next; // Node pointer to point to next node
9+
int data; // Integer stored in each node
10+
public:
11+
Node(int x){
12+
data = x; // storing the data
13+
next = NULL; // pointer to next node is null by default
14+
}
15+
};
16+
// Implementation of a Linked List Class
17+
class SingleLinkedList{
18+
private:
19+
int listsize; // variable to store size of list
20+
Node *head; // Node pointer to point to head node
21+
public:
22+
// constructor to initialise the list
23+
SingleLinkedList(){
24+
head = NULL;
25+
listsize = 0;
26+
}
27+
// Function to insert an element at the start
28+
void insertionAtHead(int data){
29+
Node *new_node = new Node(data); // create a new node
30+
// if the list is empty new node is head node
31+
if(head == NULL){
32+
head = new_node;
33+
}
34+
else{
35+
// link the new node to head
36+
new_node->next = head;
37+
head = new_node;
38+
}
39+
listsize++;
40+
}
41+
// Function to insert an element at the end
42+
void insertionAtTail(int data){
43+
Node *new_node = new Node(data); // Create a new node
44+
// if the list is empty new node is head node
45+
if (head == NULL){
46+
head = new_node;
47+
}
48+
else{
49+
// link the last node of the linked list to the new node
50+
Node *curr = head; //current pointer to head node
51+
while(curr->next != NULL){
52+
// traverse till last node is reached
53+
curr = curr->next;
54+
}
55+
curr->next = new_node;
56+
listsize++;
57+
}
58+
}
59+
// Function to insert at a user specified position
60+
void insertionAtPos(int pos, int data){
61+
if (pos < 0 && pos > listsize + 1){
62+
// check is pos is a valid position in the list
63+
cout << pos << " does not exist in the list" << endl;
64+
}
65+
else{
66+
if (pos == 0){
67+
// insert at head
68+
insertionAtHead(data);
69+
}
70+
else if (pos == listsize + 1){
71+
//insertion at tail
72+
insertionAtTail(data);
73+
}
74+
else{
75+
// insert anywhere else
76+
Node *new_node = new Node(data); // new node
77+
Node *curr = head; // pointer to the head node
78+
Node *prev = head; //pointer to store previous node
79+
int count = 0; //count positions till pos
80+
while(curr != NULL){
81+
if(count == pos){break;}
82+
count++;
83+
prev = curr;
84+
curr = curr->next;
85+
}
86+
// insertion between prev and curr if pos is found
87+
if (curr != NULL)
88+
{
89+
prev->next = new_node;
90+
new_node->next = curr;
91+
}
92+
}
93+
listsize++;
94+
}
95+
}
96+
//Function to delete head node of a linked list
97+
void deletionAtHead(){
98+
if(head == NULL){
99+
cout << "List is empty"<< endl;
100+
}
101+
else{
102+
cout << head->data << " has been deleted" << endl;
103+
Node *after = head->next; //pointer to point to next of head node
104+
head->next = NULL; //de-link head node from the rest of the list
105+
head = after; //set next node as the head node, or NULL
106+
listsize--;
107+
}
108+
}
109+
// Function to delete the tail node of a linked list
110+
void deletionAtTail(){
111+
if (head == NULL){
112+
cout << "List is empty" << endl;
113+
}
114+
else{
115+
//traverse to the last node
116+
Node *curr = head; // pointer to the head node
117+
Node *prev; //pointer to the second last node after traversal
118+
while(curr->next != NULL){
119+
//traverses till last node
120+
prev = curr;
121+
curr = curr->next;
122+
}
123+
//deletion of curr
124+
cout << curr->data << " has been deleted" << endl;
125+
prev->next = NULL;
126+
listsize--;
127+
}
128+
}
129+
//Function to delete key specified by user
130+
void deleteKey(int key){
131+
if (head == NULL){
132+
cout << "List is empty" << endl;
133+
}
134+
else{
135+
//if key is at head
136+
if(head->data == key){deletionAtHead();}
137+
else{
138+
//traverse to the last node
139+
Node *curr = head; // pointer to the head node
140+
Node *prev; //pointer to the second last node after traversal
141+
while (curr != NULL){
142+
if(curr->data == key){break;}
143+
prev = curr;
144+
curr = curr->next;
145+
}
146+
if(curr != NULL){
147+
//deletion of curr
148+
cout << curr->data << " has been deleted" << endl;
149+
Node *temp = curr->next;
150+
curr->next = NULL;
151+
prev->next = temp;
152+
}
153+
else{
154+
cout << key << " does not exist" << endl;
155+
}
156+
}
157+
}
158+
}
159+
// Function to display the linked list
160+
void display(){
161+
Node *curr = head;
162+
while(curr != NULL){
163+
cout << curr->data << "-->";
164+
curr = curr->next;
165+
}
166+
cout << "Null" << endl;
167+
}
168+
};
169+
170+
int main(){
171+
SingleLinkedList L;
172+
L.insertionAtHead(1);
173+
L.insertionAtTail(2);
174+
L.insertionAtPos(3,3);
175+
L.insertionAtTail(4);
176+
L.display();
177+
L.deletionAtHead();
178+
L.deletionAtTail();
179+
L.deleteKey(3);
180+
L.display();
181+
return 0;
182+
}

Cpp/QueueUsingArray.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Implementation of a Queue using Array
2+
#include <iostream>
3+
using namespace std;
4+
5+
class Queue{
6+
public:
7+
int front, rear, size;
8+
int *arr; // the array used to implement the queue
9+
int maxlen;
10+
public:
11+
Queue(int m){
12+
maxlen = m;
13+
arr = new int[m];
14+
front = 0; // set to the first index of arr
15+
rear = m - 1; // set to the last index of arr
16+
size = 0;
17+
}
18+
// Insert into the Queue (EnQueue Operation)
19+
void enqueue(int data){
20+
// Check for overflow: Front reaches Rear
21+
if(size == maxlen){
22+
cout << "Queue Overflow Error" << endl;
23+
}
24+
else{
25+
rear = (rear + 1) % maxlen; // Incrementing Rear
26+
// Item is added to the Rear of the Queue
27+
arr[rear] = data;
28+
size++;
29+
}
30+
}
31+
// Display front element of the queue
32+
void displayFront(){
33+
cout << arr[front] << endl;
34+
}
35+
// Display rear element of the queue
36+
void displayRear(){
37+
cout << arr[rear] << endl;
38+
}
39+
// Delete an element from the Queue (DeQueue Operation)
40+
void dequeue(){
41+
// Check for underflow: Front exceeds Rear
42+
if(size == 0){
43+
cout << "Queue Underflow Error" << endl;
44+
}
45+
else{
46+
// Item is removed from the Front of the Queue
47+
cout << arr[front] << " was deleted" << endl;
48+
front = (front + 1) % maxlen; // Incrementing Front
49+
size--;
50+
}
51+
}
52+
};
53+
54+
int main(){
55+
Queue q(5);
56+
q.enqueue(1);
57+
q.enqueue(2);
58+
q.displayFront();
59+
q.displayRear();
60+
q.dequeue();
61+
q.enqueue(3);
62+
q.displayFront();
63+
q.displayRear();
64+
return 0;
65+
}

Cpp/QueueUsingLL.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Implementation of Queue using Linked Lists
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
// Node class
7+
class Node{
8+
public:
9+
int data;
10+
Node *next;
11+
public:
12+
Node(int x){
13+
data = x;
14+
next = NULL;
15+
}
16+
};
17+
18+
// Implementing Queue
19+
class Queue{
20+
public:
21+
int size, maxlen; // size and max length of the queue
22+
Node *front, *rear; // Node pointers to point to front and rear nodes
23+
public:
24+
Queue(int m){
25+
// Initially Queue is empty
26+
size = 0;
27+
maxlen = m;
28+
front = rear = NULL;
29+
}
30+
// Insert an element at Rear (Enqueue operation)
31+
void enqueue(int data){
32+
// check if the queue is full
33+
if(size == maxlen){
34+
cout << "Queue Overflow Error" << endl;
35+
}
36+
else{
37+
// insert element at rear pointer
38+
Node *new_node = new Node(data);
39+
new_node->next = rear;
40+
rear = new_node; // rear is always newly inserted node
41+
if(size == 0){
42+
// make front pointer the first inserted node
43+
front = new_node;
44+
}
45+
}
46+
size++;
47+
}
48+
// display Rear of the Queue
49+
void displayRear(){
50+
cout << rear->data << endl;
51+
}
52+
// display Front of the Queue
53+
void displayFront()
54+
{
55+
cout << front->data << endl;
56+
}
57+
// Delete an element from the front of the Queue (Dequeue operation)
58+
void dequeue(){
59+
Node *curr = rear;
60+
Node *prev;
61+
while (curr != front){
62+
prev = curr;
63+
curr = curr->next;
64+
}
65+
cout << curr->data << " has been deleted" << endl;
66+
front = prev; // front is always node to be removed
67+
prev->next = NULL;
68+
size--;
69+
}
70+
};
71+
72+
int main(){
73+
Queue q(5);
74+
q.enqueue(1);
75+
q.enqueue(2);
76+
q.enqueue(3);
77+
q.enqueue(4);
78+
q.displayRear();
79+
q.displayFront();
80+
q.dequeue();
81+
q.displayRear();
82+
q.displayFront();
83+
return 0;
84+
}

Cpp/StackUsingArray.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Implement a stack using arrays in C++
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
class Stack {
7+
// class variables for the stack
8+
private: // access specifiers so these variables cannot be accessed outside
9+
int maxlen, top;
10+
int *arr; // serves as the stack
11+
// constructor to initialise the stack
12+
public:
13+
Stack(int maxlength){
14+
maxlen = maxlength; // maximum size of the stack
15+
top = -1; // keeps track of the last element (-1 represents empty)
16+
arr = new int[maxlength]; // stack
17+
}
18+
// insert element into the "top" of the stack
19+
void push(int data){
20+
if (top != maxlen - 1) {
21+
// condition to check if stack is full
22+
arr[top] = data;
23+
cout << data << " has been inserted" << endl;
24+
top++; // one element has been added
25+
}
26+
else {
27+
cout << "Stack Overflow Error" << endl;
28+
}
29+
}
30+
// display the last entered element of the stack
31+
void peek(){
32+
cout << arr[top] << endl;
33+
}
34+
// delete an element from the "top" of the stack
35+
void pop(){
36+
if(top == -1){
37+
cout << "Stack Underflow Error" << endl;
38+
}
39+
else {
40+
cout << arr[top] << " has been deleted" << endl;
41+
top--; // one element has been decreased
42+
}
43+
}
44+
};
45+
46+
int main(){
47+
Stack st(5);
48+
st.push(1);
49+
st.pop();
50+
st.pop();
51+
return 0;
52+
}

0 commit comments

Comments
 (0)