Pointer
#include <iostream>
using namespace std;
void swapNumbers(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main(){
int x = 5;
int y = 10;
cout<<"Before swapping: x = "<<x<<", y = "<<y<<endl;
swapNumbers(&x, &y);
cout<<"After swapping: x = "<<x<<", y = "<<y<<endl;
}
Linked List
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
class LinkedList{
private:
Node *head;
public:
LinkedList(){
head = NULL;
}
void insertEnd(int value){
Node *newNode = new Node;
newNode->data = value;
newNode->next = NULL;
if(head == NULL){
head = newNode;
return;
}
Node *temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
}
void insertBegin(int value){
Node *newNode = new Node;
newNode->data = value;
newNode->next = head;
head = newNode;
}
void deleteValue(int value){
if(head == NULL){
cout<<"List is empty!"<<endl;
return;
}
if(head->data == value){
Node *temp = head;
head = head->next;
delete temp;
return;
}
Node *temp = head;
Node *prev = NULL;
while(temp != NULL && temp->data != value){
prev = temp;
temp = temp->next;
}
if(temp == NULL){
cout<<"Value not found in the list!"<<endl;
return;
}
prev->next = temp->next;
delete temp;
}
bool search(int value){
Node *temp = head;
while(temp != NULL){
if(temp->data == value){
cout<<value<<" found in the list."<<endl;
return true;
}
temp = temp->next;
}
cout<<value<<" not found in the list."<<endl;
return false;
}
void display(){
Node *temp = head;
while(temp != NULL){
cout<<temp->data;
if(temp->next){
cout<<" -> ";
}
temp = temp->next;
}
cout<<endl;
}
};
int main(){
LinkedList ll;
[Link](10);
[Link](20);
[Link](5);
[Link](30);
cout<<"Linked List: ";
[Link]();
[Link](20);
cout<<"Linked List after deletion: ";
[Link]();
[Link](10);
[Link](50);
}
Stack
#include <iostream>
using namespace std;
class Stack{
private:
const static int maxSize = 100;
int arr[maxSize];
int top;
public:
Stack(){
top = -1;
}
void push(int value){
if(top == 4){
cout<<"Stack Overflow!"<<endl;
return;
}
arr[++top] = value;
}
void pop(){
if(isEmpty()){
cout<<"Stack Underflow!"<<endl;
return;
}
top--;
}
void peek(){
if(isEmpty()){
cout<<"No value on Stack."<<endl;
return;
}
cout<<"Top element: "<<arr[top]<<endl;;
}
void display(){
for(int i = top; i >= 0; i--){
cout<<arr[i]<<" ";
}
cout<<endl;
}
bool isEmpty(){
return top == -1;
}
};
int main(){
Stack s;
[Link](10);
[Link](20);
[Link](30);
cout<<"Stack elements: ";
[Link]();
[Link]();
[Link]();
cout<<"Stack after pop: ";
[Link]();
[Link]();
[Link]();
}
Queue
#include <iostream>
using namespace std;
class Queue{
private:
const static int maxSize = 100;
int arr[maxSize];
int front;
int rear;
public:
Queue(){
front = -1;
rear = -1;
}
void enq(int value){
if(rear == 4){
cout<<"Queue Overflow!"<<endl;
return;
}
if(front == -1){
front = 0;
}
arr[++rear] = value;
}
void deq(){
if(isEmpty()){
cout<<"Queue Underflow!"<<endl;
return;
}
front++;
if(front > rear){
front = -1;
rear = -1;
}
}
void peek(){
if(isEmpty()){
cout<<"Queue is empty!"<<endl;
return;
}
cout<<"Front element: "<<arr[front]<<endl;
}
void display(){
if(isEmpty()){
cout<<"Queue is empty!"<<endl;
return;
}
for(int i = front; i <= rear; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
bool isEmpty(){
return (front == -1 || front > rear);
}
};
int main(){
Queue q;
[Link](10);
[Link](20);
[Link](30);
cout<<"Queue elements: ";
[Link]();
[Link]();
[Link]();
cout<<"Queue after dequeue: ";
[Link]();
}