Namespace Int Void Int Int
Namespace Int Void Int Int
case 3: display(stack);
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
2. Queue using arrays
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAX 5
int front=-1, rear=-1;
case 2: delete_element(queue);
break;
case 3: display(queue);
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
3. Stack using Linked List
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *top = NULL;
void push(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
if(top == NULL){
top = temp;
top -> next = NULL;
}
else{
temp -> next = top;
top = temp;
}
}
void pop(){
NODE *temp;
temp = top;
if(top == NULL)
cout << "The stack is empty." << endl;
else{
top = top -> next;
free(temp);
}
}
void display(){
cout << "The Stack: " << endl;
NODE *temp;
temp = top;
if(top == NULL)
cout << "is empty" << endl;
else{
while(temp -> next != NULL){
cout << temp -> data << " ";
temp = temp -> next;
}
cout << temp -> data;
cout << endl;
}
}
int main(){
int ch;
int data;
do{
cout << "Menu: " << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to push: ";
cin >> data;
push(data);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
4. Queue using Linked List
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *front, *rear;
void insert(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
if(front == NULL){
front = temp;
front -> next = NULL;
rear = front;
}
else{
rear -> next = temp;
temp -> next = NULL;
rear = temp;
}
}
void remove(){
NODE *temp;
temp = front;
if(front == NULL)
cout << "The Queue is empty." << endl;
else{
front = front -> next;
free(temp);
}
}
void display(){
NODE *temp;
temp = front;
cout << "The Queue: " << endl;
if(front == NULL)
cout << "is empty." << endl;
else{
while(temp -> next != NULL){
cout << temp -> data << " ";
temp = temp-> next;
}
cout << temp -> data << endl;
}
}
int main(){
int ch;
int data;
do{
cout << "Menu: " << endl;
cout << "1. Insert" << endl;
cout << "2. Remove" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to push: ";
cin >> data;
insert(data);
break;
case 2: remove();
break;
case 3: display();
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
5. Singly Linked List
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *start;
void insert(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
if(start == NULL){
start = temp;
start -> next = NULL;
}
else{
temp -> next = start;
start = temp;
}
}
void remove(int newdata){
NODE *temp, *temp2;
temp = start;
temp2 = start;
if(start == NULL)
cout << "The List is empty." << endl;
else{
while(temp -> data != newdata){
temp = temp-> next;
}
while(temp2 -> next != temp)
temp2 = temp2 -> next;
int main(){
int ch;
int data;
do{
cout << "Menu: " << endl;
cout << "1. Insert" << endl;
cout << "2. Remove" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to insert: ";
cin >> data;
insert(data);
break;
case 3: display();
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
struct node{
int data;
struct node *next, *prev;
};
typedef struct node NODE;
NODE *start;
void insert(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
if(start == NULL){
start = temp;
start -> next = NULL;
start -> prev = NULL;
}
else{
start -> prev = temp;
temp -> next = start;
temp -> prev = NULL;
start = temp;
}
}
int main(){
int ch;
int data, pos;
do{
cout << "Menu: " << endl;
cout << "1. Insert" << endl;
cout << "2. insert_mid" << endl;
cout << "3. insert_end" << endl;
cout << "4. Remove" << endl;
cout << "5. Display" << endl;
cout << "6. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to insert: ";
cin >> data;
insert(data);
break;
case 5: display();
break;
case 6: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=6);
}
7. Tree Traversal
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
char data;
struct node *left, *right;
};
typedef struct node NODE;
NODE* makeNode(char n){
NODE *newn;
newn = (NODE*)malloc(sizeof(NODE));
newn -> data = n;
newn -> left = NULL;
newn -> right = NULL;
return newn;
}
void preOrder(NODE *temp){
if(temp != NULL){
cout << temp -> data << endl;
preOrder(temp -> left);
preOrder(temp -> right);
}
}
void inOrder(NODE *temp){
if(temp != NULL){
inOrder(temp -> left);
cout << temp -> data << endl;
inOrder(temp -> right);
}
}
void postOrder(NODE *temp){
if(temp != NULL){
postOrder(temp -> left);
postOrder(temp -> right);
cout << temp -> data << endl;
}
}
int main(){
NODE *tree;
tree = makeNode('A');
tree -> left = makeNode('B');
tree -> right = makeNode('C');
cout << "PreOrder: " << endl;
preOrder(tree);
cout << "InOrder: " << endl;
inOrder(tree);
cout << "PostOrder: " << endl;
postOrder(tree);
}
8. Selection Sort
#include<iostream>