You are on page 1of 19

Stack using array

AIM:
To write a C++ program to implement stack operations using array.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Define necessary data members and member functions in the class
Stack.
STEP 3: Enter the choice of stack menu.
STEP 4: If choice = 1, call push( ) to insert the new element.
STEP 5: If choice = 2, call pop( ) to delete the element.
STEP 6: If choice = 3, call display( ) to show the stack list.
STEP 7: If choice is any other values exit the program.
STEP 8: Stop the Program.

15. Stack using array

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define SIZE 5

class Stack {
private:
int item, i;
int data[SIZE];
int top;

public:

Stack() {
top = 0;

void push() {
if (top == SIZE)
cout << "\n\t Stack is Full!";
else {
cout << "\nEnter the value to be pushed : ";
cin>>item;
cout << "\n Position : " << top << ", Pushed Value :" <<
item;
data[top++] = item;
}
}

void pop() {
if (top == 0)
cout << "\n Stack is Empty!";
else {
--top;
cout << "\n Position : " << top << ", Popped Value :" <<
data[top];
}
}

void display() {
cout << "\n Stack Size : " << top;
for (i = (top - 1); i >= 0; i--)
cout << "\n Position : " << i << ", Value :" << data[i];
}
};

int main() {
clrscr();
int choice, exit = 1;
Stack S;
cout << "\n\t\t\t Stack Using Array";
do {
cout << "\n\n Main Menu";

cout << "\n\n1.Push \n2.Pop \n3.Display \nOthers to exit";


cout << "\n\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
S.push();
break;
case 2:
S.pop();
break;
case 3:
S.display();
break;
default:
exit = 0;
break;
}
} while (exit);

return 0;
}
Stack using linked list

AIM:
To write a C++ program to implement stack operations using linked
list.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Define a structure node with two members data and next
STEP 3: Define necessary data members and member functions in the class
Stack.
STEP 4: Enter the choice of stack menu.
STEP 5: If choice = 1, call push( ) to insert the new element.
STEP 6: If choice = 2, call pop( ) to delete the element.
STEP 7: If choice = 3, call display( ) to show the stack list.
STEP 8: If choice is any other values exit the program.
STEP 9: Stop the Program.

16. Stack using linked list

#include<iostream.h>
#include<conio.h>

struct node
{
int data;
struct node *next;
};

class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push()
{
int value;
struct node *ptr;
cout<<"\nPUSH Operation\n";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
}

void pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
}
temp=top;
top=top->next;
cout<<"\nPOP Operation........\nPoped value is "<<temp->data;
delete temp;
}
void show() // to show the stack
{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}
};

int main()
{
clrscr();
stack s;
int choice;
while(1)
{
cout<<"\n\n\t\tSTACK USING LINKED LIST\n\n";
cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";
cout<<"\n\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
return 0;
break;
default:
cout<<"\nPlease enter correct choice(1-4)!!";
break;
}
}
return 0;
}
Queue using array

AIM:
To write a C++ program to implement queue operations using array.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Enter the choice of queue menu.
STEP 3: If choice = 1, call ins( ) to insert the queue element in the rear.
STEP 4: If choice = 2, call del( ) to delete the element from the front.
STEP 5: If choice = 3, call display( ) to show the list of elements in the
queue.
STEP 6: If choice is any other values exit the program.
STEP 7: Stop the Program.

17.QUEUE USING ARRAY

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
int q[SIZE],front=0,rear=0;
void main()
{
int ch;
clrscr();
void ins();
void del();
void display();
cout<<"\t\t\t QUEUE USING ARRAY";
while(1)
{
cout<<"\n\n 1.Insert element";
cout<<"\n 2.Delete element";
cout<<"\n 3.Display";
cout<<"\n 4.Exit";
cout<<"\n\n Enter your choice:";
cin>>ch;
clrscr();
switch(ch)
{
case 1:
ins();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"\n Invalid choice";
}
}
}
void ins()
{
int n;
if (rear==SIZE && front==0)
cout<<"\nQueue is full";
else
{
cout<<"\nEnter the element to insert:";
cin>>n;
q[rear]=n;
}
rear++;
}
void del()
{
int n,i;
if (front==rear)
cout<<"\nQueue is empty";
else
{
n=q[front];
front++;
cout<<"\n"<<n<<" - removed from the queue\n";
}
}
void display()
{
int i,temp=front;
if (front==rear)
cout<<"\nQueue is empty";
else
{
cout<<"\n Elements in the queue:";
for(i=temp;i<rear;i++)
{
cout<<q[i]<<" ";
}
}}

Queue using linked list

AIM:
To write a C++ program to implement queue operations using linked
list.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Define a structure node with two members data and next
STEP 4: Enter the choice of queue menu.
STEP 5: If choice = 1, call ins( ) to insert the element in the rear
position.
STEP 6: If choice = 2, call pop( ) to delete the element from the front.
STEP 7: If choice = 3, call display( ) to show the stack list.
STEP 8: If choice is any other values exit the program.
STEP 9: Stop the Program.

18.QUEUE USING LINKED LIST

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

struct node
{
int data;
struct node *next;
}*front=NULL,*rear,*temp;

void ins()
{
temp=new node;
cout<<"Enter data:";
cin>>temp->data;
temp->next=NULL;

if(front==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
}

void del()
{
if(front==NULL)
cout<<"Queue is empty\n";
else
{
temp=front;
front=front->next;
cout<<"Deleted node is "<<temp->data<<"\n";
delete(temp);
}
}

void dis()
{
if(front==NULL)
cout<<"Queue is empty\n";
else
{
temp=front;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
}
}

int main()
{
clrscr();
int ch;
cout<<"Queue Using Linked List";
while(1)
{
cout<<"\n\n*** Menu
***"<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
cout<<"\n\nEnter your choice(1-4):";
cin>>ch;
cout<<"\n";

switch(ch)
{
case 1: ins();
break;
case 2: del();
break;
case 3: dis();
break;
case 4: exit(0);
break;
default: cout<<"Wrong Choice!!!";
}
}
getch();
return 0;
}

Circular Queue

AIM:
To write a C++ program to implement circular queue operations.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define necessary data members and member functions in the class
C_Queue.
STEP 4: Enter the choice of circular queue menu.
STEP 5: If choice = 1, call insert( ) to insert the element .
STEP 6: If choice = 2, call pop( ) to delete the element.
STEP 7: If choice = 3, call display( ) to show the circular queue.
STEP 8: If choice is any other values exit the program.
STEP 9: Stop the Program.

19.CIRCULAR QUEUE

#include <iostream.h>
#include<conio.h>
#define MAX 5

class C_Queue
{
private:
int *data;
int front, rear;
public:
C_Queue()
{
data = new int [MAX];
rear = front = -1;
}

void insert(int item)


{
if ((front == 0 && rear == MAX-1) || (front == rear+1))
{
cout<<"\n Queue Overflow \n";
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
data[rear] = item ;
}

void del()
{
if (front == -1)
{
cout<<"\nQueue Underflow\n";
return ;
}
cout<<"\n Element deleted from queue is :
"<<data[front]<<endl;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}

void display()
{
int f_pos = front, r_pos = rear;
if (front == -1)
{
cout<<"\n Queue is empty\n";
return;
}
cout<<"\n Queue elements : ";
if (f_pos <= r_pos)
{
while (f_pos <= r_pos)
{
cout<<data[f_pos]<<" ";
f_pos++;
}
}
else
{
while (f_pos <= MAX - 1)
{
cout<<data[f_pos]<<" ";
f_pos++;
}
f_pos = 0;
while (f_pos <= r_pos)
{
cout<<data[f_pos]<<" ";
f_pos++;
}
}
cout<<endl;
}
};

int main()
{
clrscr();
int choice, item;
C_Queue cq;
cout<<"\t\t\t CIRCULAR QUEUE \n";
do
{
cout<<"\n1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter the element to insert : ";
cin>>item;
cq.insert(item);
break;
case 2:
cq.del();
break;
case 3:
cq.display();
break;
case 4:
break;
default:
cout<<"Wrong choice\n";
}
}
while(choice != 4);
getch();
return 0;
}
Index for SQL

Consider the tables given below and answer the questions that follow:

Table: Employee

No Name Salary Zone Age Grade Dept


1 Mukul 30000 West 28 A 10
2 Kritika 35000 Centre 31 A 10
3 Naveen 32000 West 40 20
4 Uday 38000 North 38 C 30
5 Nupur 32000 East 26 B 20

Table: Department

Dept DName MinSal MaxSal HOD


10 Sales 25000 32000 1
20 Finance 30000 50000 5
30 Admin 25000 40000 7

Write SQL commands to:

Create Table
• Create the table Employee.
• Create the table Department.
Insert data in a table
• Insert data in the table Employee
• Insert data in the table Department.
Simple Select
• Display the details of all the employees.
Conditional Select using Where Clause
• Display the details of all the employees who are below 30 years of age.
Using NULL
• Display the details of all the employees whose Grade is not NULL.
Using DISTINCT Clause
• Display the names of various zones from the table Employee. A zone name should
appear only once.
Using Logical Operators (NOT, AND, OR)
• Display the details of all the employees of department 10 who are above 30 years of
age.
Using IN Operator
• Display the names of all the employees who are working in department 20 or 30.
(Using IN operator)
Using BETWEEN Operator
• Display the details of all the employees whose grade is between ‘A’ and ‘C’.
(Using BETWEEN operator)
Using LIKE Operator
• Display the name, salary, and age of all the employees whose names start with ‘M’.
Using Aggregate functions
• Display the sum and average of the salaries of all the employees.
• Display the highest and the lowest salaries being paid in department 10.
Using ORDER BY clause
• Display the details of all the employees in the ascending order of their grades and
within grades in the descending order of their salaries.
Using GROUP BY clause
• Display the highest salary, lowest salary, and average salary of each zone.
Using UPDATE, DELETE, ALTER TABLE
• Put the grade B for all those whose grade is A.
• Increase the salary of all the employees above 30 years of age by 10%.
JOIN of two tables
• Display the details of all the employees who work in Sales department.
• Display the Name and Department Name of all the employees.

You might also like