Data Structure
Data Structure
PROGRAMME – BCA
SEMESTER- II
DATA STRUCTURE USING C(BCA 204)
UNIT II
Stack ADT
UNIT II
DATA STRUCTURE USING C BCA SEM II
1. createStack function– This function takes the maximum number of elements (maxElements)
the stack can hold as an argument, creates a stack according to it and returns a pointer to the
stack. It initializes Stack S using malloc function and its properties.
2. push function - This function takes the pointer to the top of the stack S and the item (element)
to be inserted as arguments. Check for the emptiness of stack
3. pop function - This function takes the pointer to the top of the stack S as an argument.
4. top function – This function takes the pointer to the top of the stack S as an argument
and returns the topmost element of the stack S.
Properties of stacks:
1. Each function runs in O(1) time.
2. It has two basic implementations
Array-based implementation – It is simple and efficient but the maximum size of the
stack is fixed.
Singly Linked List-based implementation – It’s complicated but there is no limit on the
stack size, it is subjected to the available memory.
UNIT II
DATA STRUCTURE USING C BCA SEM II
/* If stack size is zero then it is empty. So we cannot pop */
if(S->size==0)
{
printf("Stack is Empty\n");
return;
}
/* Removing an element is equivalent to reducing its size by one */
else
{
S->size--;
}
return;
}
int top(Stack *S)
{
if(S->size==0)
{
printf("Stack is Empty\n");
exit(0);
}
/* Return the topmost element */
return S->elements[S->size-1];
}
void push(Stack *S,int element)
{
/* If the stack is full, we cannot push an element into it as there is no space for it.*/
if(S->size == S->capacity)
{
printf("Stack is Full\n");
}
else
{
/* Push an element on the top of it and increase its size by one*/
S->elements[S->size++] = element;
}
UNIT II
DATA STRUCTURE USING C BCA SEM II
return;
}
int main()
{
Stack *S = createStack(5);
push(S,7);
push(S,5);
push(S,21);
push(S,-1);
printf("Top element is %d\n",top(S));
pop(S);
printf("Top element is %d\n",top(S));
pop(S);
printf("Top element is %d\n",top(S));
pop(S);
printf("Top element is %d\n",top(S));
}
Evaluation of Arithmetic Expressions
This notation is known as postfix notation and is evaluated as described above. We shall
shortly show how this form can be generated using a stack.
Basically there are 3 types of notations for expressions. The standard form is known as the
infix form. The other two are postfix and prefix forms.
Note that all infix expressions can not be evaluated by using the left to right order of the
operators inside the expression. However, the operators in a postfix expression are ALWAYS in
the correct evaluation order. Thus evaluation of an infix expression is done in two steps.
The first step is to convert it into its equivalent postfix expression. The second step involves
UNIT II
DATA STRUCTURE USING C BCA SEM II
evaluation of the postfix expression. We shall see in this section, how stacks are useful in
carrying out both the steps. Let us first examine the basic process of infix to postfix
conversion.
Example 2:
( A + B ) * C Infix form
( A B + ) * C Convert the addition
(A B + ) C * Convert multiplication
A B + C * Postfix form
No need of parenthesis anywhere
Example 3:
a + (( b * c ) / d ) 5
a + ( ( b c * ) /d )
(precedence of * and / are same and they are left associative)
a+(bc*d/)
abc*d/+
Queue ADT
• It is a linear data structure that maintains a list of elements such that insertion happens at
rear end and deletion happens at front end.
UNIT II
DATA STRUCTURE USING C BCA SEM II
Queue implementation:
Insert operation(Enqueue)
• It includes checking whether or not the queue pointer rear is pointing at the upper bound
of the array. If it is not, rear is incremented by 1 and the new item is placed at the end of
the queue.
• Algorithm
Step 1 : Start
Step 10 : Stop
UNIT II
DATA STRUCTURE USING C BCA SEM II
• Delete operation(Dequeue)
– It includes checking whether or not the queue pointer front is already pointing at
NULL. . If it is not, the item that is being currently pointed is removed from the
queue and front pointer is incremented by 1.
– Algorithm
Step 1 : Start
Step 2 : If front = NULL and rear =NULL goto Step 3 else goto Step 4
Step 4 : If front != NULL and front = rear goto Step 5 else goto Step 8
Step 5 : Set I = queue[front]
Step 10 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int queue[100];
int front=-1;
int rear=-1;
void insert(int);
int del();
void display();
void main()
{
int choice;
int num1=0,num2=0;
while(1)
UNIT II
DATA STRUCTURE USING C BCA SEM II
{
printf(“\n Select a choice from the following : “);
printf(“\n[1] Add an element into the queue”);
printf(“\n[2] Remove an element from the queue”);
printf(“\n[3] Display the queue elements”);
printf(“\n[4] Exit\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
{
printf(“\nEnter the element to be added :”);
scanf(“%d”,&num1);
insert(num1); break;
}
{
num2=del();
if(num2==9999);
else
printf(“\n %d element removed from the queue”);
getch(); break;
}
case 3:
{
display();
getch();
break;
}
Case 4:
Exit(1);
UNIT II
DATA STRUCTURE USING C BCA SEM II
break;
default:
printf(“\nInvalid choice”);
break;
}
}
}
void display()
{
int i;
if(front==-1)
{
printf(“Queue is empty”);
return;
}
printf(“\n The queue elements are :\n”);
for(i=front;i<=rear;i++)
printf(“\t%d”,queue[i]);
}
void insert(int element)
{
if(front==-1 )
{
front = rear = front +1;
queue[front] = element;
return;
}
if(rear==99)
{
printf(“Queue is full”);
getch();
UNIT II
DATA STRUCTURE USING C BCA SEM II
return;
}
rear = rear +1;
queue[rear]=element;
}
void insert(int element)
{
if(front==-1 )
{
front = rear = front +1;
queue[front] = element;
return;
}
if(rear==99)
{
printf(“Queue is full”);
getch();
return;
}
rear = rear +1;
queue[rear]=element;
}
Linked Implementation of Queues:
• It is based on the dynamic memory management techniques which allow allocation and
de-allocation of memory space at runtime.
Insert operation:
Step 1: Start
Step 10 : Stop
Delete operation:
Step 1 : Start
Step 7 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
UNIT II
DATA STRUCTURE USING C BCA SEM II
struct queue
{
int element;
struct queue *next;
};
struct queue *front = NULL;
struct queue *rear = NULL;
void insert(int);
int del();
void display();
void main()
{
int choice;
int num1=0,num2=0;
while(1)
{
printf(“\n Select a choice from the following : “);
printf(“\n[1] Add an element into the queue”);
printf(“\n[2] Remove an element from the queue”);
printf(“\n[3] Display the queue elements”);
printf(“\n[4] Exit\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
{
printf(“\nEnter the element to be added :”);
scanf(“%d”,&num1);
insert(num1); break;
}
case 2:
{
num2=del();
UNIT II
DATA STRUCTURE USING C BCA SEM II
if(num2==9999)
;
else
printf(“\n %d element removed from the queue”);
getch();
break;
}
case 3:
{
display();
getch();
break;
}
case 4:
exit(1);
break;
default:
printf(“\nInvalid choice”);
break;
}
}
}
UNIT II
DATA STRUCTURE USING C BCA SEM II
rear→next = ptr;
ptr→next =NULL;
rear = ptr;
}
}
int del()
{
int i;
if(front == NULL) /*checking whether the queue is empty*/
{
return(-9999);
}
else
{
i = front→element;
front = front→next;
return i;
}
}
void display()
{
struct queue *ptr = front;
if(front==NULL)
{
printf(“Queue is empty”);
return;
}
UNIT II
DATA STRUCTURE USING C BCA SEM II
UNIT II