DS Chapter 1 and 2 Notes
DS Chapter 1 and 2 Notes
Q.6] Explain concept of stack, what are the various operation performed on the
data structure.
→
― Stack : the stack is an organisation that uses processing Last-In-
First-Out (LIFO). The data which is added at the end is always
removed first for processing.
1) Push() : Insertion/addition a value at the stack top.
2) Pop() : Deleting a value from the stack top.
3) Peep() : It returns the value from the stack top but does not
delete the value.
4) isEmpty() : It returns output as there if the stack is empty, else
returns false.
5) isFull() : It returns output as true if the stack is full, else it returns
false.
― There are two ways for memory allocation for the stack : Static
and Dynamic
1) Static memory allocation is done with the use of array.
2) Dynamic memory allocation is done with the use of linked
list.
Q.8] Write down the ADT for the stack. Write down program to implement
following operation on a stack using an array. 1)push 2)pop 3)peep 4)display
→ A]
Abstract typedef < eltype > stack(s)
Abstract empty (s)
Stack < eltype > s
Postcondition empty == true
If (length(s)==0)
Abstract full (s)
Stack < eltype > s
Precondition empty == false
Postcondition = true, If (length (s) == max capacity of the storage)
Abstract eltype pop (s)
Stack (eltype) s
Precondition empty (s) = false
Postcondition pop = first (s)
Abstract eltype push (x,s)
Stack (eltype) s
eltype x
postcondition: s = s + < x >
B] Program:
#include<stdio.h>
#include<conio.h>
#define MAX 100
int TOP=-1;
int STACK[MAX];
void push(int val);
void pop();
void peep();
void display();
void main()
{
int choice,a;
clrscr();
do
{
printf("Enter your choice \n");printf("1.push \t 2.pop \t 3.peep \t 4.display \n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter new element");scanf("%d",&a);push(a);break;
case 2: pop();break;
case 3: peep();break;
case 4: display();break;
default : printf("It is wrong choice");
}
}while(choice!=5);
getch();
}
void push(int val)
{
if(TOP==MAX-1)
{printf("Overflow Condition \n");}
else
{TOP++;STACK[TOP]=val;printf("Elemented is inserted \n");}
}
void pop()
{
int val;
if(TOP==-1)
{printf("Underflow Condition \n");}
else
{val=STACK[TOP];TOP--;printf("Poped Element is %d \n",val);}
}
void peep()
{
int val;
if(TOP==-1)
{printf("Underflow Condition \n");}
else
{val=STACK[TOP];printf("Peeped element is %d \n",val);}
}
void display()
{
int val,i;
if(TOP==-1)
{printf("It is empty stack \n");}
else
{
for(i=TOP;i>=0;i--)
{printf("%d \n",STACK[i]);}
}
}
Q.11] Write down program to perform operation of linear queue with array.
→ Program:
#include<stdio.h>
#include<conio.h>
#define MAX 100
int FRONT=-1; int REAR=-1;
int Queue[MAX];
void insert(int val);
void remove1();
void display();
void main()
{
int choice,a;
clrscr();
do
{
printf("Enter your choice \n");
printf("1.insert \t2.remove \t3.display \n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter new element \n");
scanf("%d",&a);
insert(a);
break;
case 2: remove1();
break;
case 3: display();
break;
default : printf("It is wrong choice \n");
}
}while(choice!=4);
getch();
}
void insert(int val)
{
if(REAR==MAX-1)
{printf("Queue is full \n");}
else if(FRONT==-1&&REAR==-1)
{FRONT=REAR=0;Queue[REAR]=val;}
else
{REAR++;Queue[REAR]=val;}
}
void remove1()
{
int val;
if(REAR==-1&&FRONT==-1)
{printf("Queue is empty \n");}
else if(REAR==FRONT)
{val=Queue[FRONT];FRONT=REAR=-1;}
else
{val=Queue[FRONT];FRONT++;}
printf("Deleted element is %d \n",val);
}
void display()
{
int i;
if(REAR==-1&&FRONT==-1)
{printf("Queue is empty \n");}
else
{
for(i=FRONT;i<=REAR;i++)
{printf("%d\n",Queue[i]);}
}
}