Unit 1
Unit 1
Unit 1
• Data Structures
– Physical implementation of an ADT
– data structures used in implementations are
provided in a language (primitive or built-in)
or are built from the language constructs (user-
defined)
– Each operation associated with the ADT is
implemented by one or more subroutines in the
implementation
The Core Operations of ADT
+A * B C A + B * C A B C * +
* + A B C (A+B) * C A B + C *
+ – A B C A – B + C A B – C +
– A + B C A – (B+C) A B C + –
Infix to Postfix Conversion
Assume 1-digit integer operands, the binary operators + - * / only, and the
string to be converted is properly formed
3. if it’s an operator –
if the stack is empty, push it on the stack
else pop operators of
greater or equal precedence and append them to the postfix string, stopping when
a ‘(‘ is reached, an operator of lower precedence is reached, or the stack is empty;
then push the operator on the stack
4. if it’s a ‘)’ – pop operators off the stack, appending them to the postfix string, until
a ‘(‘ is encountered and pop the ‘(‘ off the stack
5. when the end of the infix string is reached – pop any remaining operators off the
stack and append them to the postfix string
Infix to Postfix Conversion (continued)
An Example: 7-(2*3+5)*(8-4/2) 723*5+842/-*-
Remaining Infix String char Stack Postfix String Rule Used
7-(2*3+5)*(8-4/2) empty null
-(2*3+5)*(8-4/2) empty 7 1
(2*3+5)*(8-4/2) - 7 3
2*3+5)*(8-4/2) -( 7 2
*3+5)*(8-4/2) -( 72 1
3+5)*(8-4/2) -(* 72 3
+5)*(8-4/2) -(* 723 3
5)*(8-4/2) -(+ 723* 3
)*(8-4/2) -(+ 723*5 1
*(8-4/2) - 723*5+ 4
(8-4/2) -* 723*5+ 3
8-4/2) -*( 723*5+ 2
-4/2) -*( 723*5+8 1
4/2) -*(- 723*5+8 3
/2) -*(- 723*5+84 1
2) -*(-/ 723*5+84 3
) -*(-/ 723*5+842 1
null empty 723*5+842/-*- 4&5
#include "stkadtch.h" int isOperator(char ch)
void findPostfix(char ine[],char pose[]) {
{ if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
char ch; int i,j = 0; return 1;
stack s; s.top = -1; return 0;
for(i = 0; ine[i] != '\0'; i++){ }
ch = ine[i];
if(ch == '(') push(&s,ch); int prec(char ch){
else if(ch == ')') { switch(ch){
while(peek(s) != '(') case '*' :
pose[j++] = pop(&s); case '/' : return 3;
pop(&s); } case '-' :
else if(isOperator(ch)){ case '+' : return 2;
if(isEmpty(s)) push(&s,ch); case '(' : return 1; }
else{ }
while(prec(ch) < prec(peek(s)))
pose[j++] = pop(&s); void main(){
push(&s,ch); }} char inexpr[20],postexpr[20]="";
else pose[j++] = ch; } printf("\n Enter Infix Expression :");
while(!isEmpty(s)) gets(inexpr);
pose[j++] = pop(&s); findPostfix(inexpr,postexpr);
pose[j] = '\0'; printf("\n postfix expression is:%s",postexpr);
} getch();
}
Postfix Expression Evaluation
Assume 1-digit integer operands, the binary operators + - * / only,
and the string to be evaluated is properly formed
Rules for processing the postfix string:
Starting from the left hand end, inspect each character of the string
1. if it’s an operand – push it on the stack
2. if it’s an operator – remove the top 2 operands from the stack,
perform the indicated operation, and push the result on the stack
Rear Front
insert
Queue operations
enqueue : adds a new element
enqueue(Q,x) add the element X to the REAR of
queue
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
10 50 13 5 -8 14
Front Rear
#include "stdio.h"
#include "conio.h" int dequeue(queue *qp)
#define MAX 5 {
typedef struct if(!isEmpty(*qp))
{ return qp->qele[qp -> front++];
int front; else
int rear; printf("\nqueue Underflow");
int qele[MAX]; return 0;
}queue; }
int isEmpty(queue q){ void enqueue(queue *qp,int ele)
if(q.rear < q.front) return 1; {
else return 0; } if(!isFull(*qp))
int isFull(queue q){ qp->qele[++qp -> rear] = ele;
if(q.rear == MAX-1) return 1; else
else return 0; } printf("\n queue Overflow");
void display(queue q){
int i; }
if(!isEmpty(q)){
printf("\n queue elements are :");
for(i = q.front; i <= q.rear; i ++)
printf("%5d",q.qele[i]);
}
else
printf("\n queue is empty"); }
int menu(){
int ch; clrscr();
printf("\n queue Operations :");
#include "qADTin.h" printf("\n\t 1 -> enqueue \n\t 2 -> dequeue
void main(){
int ch,ele; \n\t 3 -> display \n\t 4 ->exit");
char che; printf("\n Enter your choice :");
queue q; q.front = 0; q.rear = -1; scanf("%d",&ch); return ch;}
do {
ch = menu();
switch(ch){
case 1: printf("\n Enter element :");
scanf("%d",&ele);
enqueue(&q,ele); break;
case 2:ele = dequeue(&q);
if(ele)
printf("\n the deleted element is %d",ele);
break;
case 3: display(q); break;
case 4: break;
default : printf("\n Invalid choice. try again ");
}
}while(ch!=4);
}
Applications of Queues
• Multiprogramming
0 1 2 3 4 5 6 7
Initially q -> rear = MAX - 1
and q -> front = MAX - 1;
When an element is
7 Inserted rear incremented by
0 rear = (rear + 1) % MAX;
When an element is deleted
front is incremented by
front = (front + 1) % MAX;
6 1
The queue is empty when
fornt = rear;
4 3
#define MAX 5 void display(queue q){
typedef struct int i;
{ if(!isEmpty(q)) {
int front; printf("\n queue elements are :");
int rear; for(i = q.front+1; i <= q.rear; (++i) %MAX)
int qele[MAX]; printf("%5d",q.qele[i]);
}queue; }
int isEmpty(queue q) else printf("\n queue is empty");
{
if(q.rear == q.front) }
return 1; int dequeue(queue *qp){
else if(!isEmpty(*qp)) {
return 0; qp -> front = (qp -> front + 1) % MAX ;
} return qp->qele[qp -> front]; }
int isFull(queue q) else printf("\nqueue Underflow");
{ return 0;
if((q.rear+1)%MAX == q.front ) }
return 1; void enqueue(queue *qp,int ele){
else if(!isFull(*qp)) {
return 0; qp -> rear = (qp -> rear + 1) % MAX;
} qp->qele[qp -> rear] = ele; }
else printf("\n queue Overflow");
}
Assignment – 1
1. Write a C program to convert a given infix expression into postfix expression
using pointers.
2. Declare a circular queue of integers such that F points to the front and R points
to the rear. Write functions
(a) To insert an element into queue
(b) To delete an element from queue.
3. Explain how is queue represented as an abstract data type. Write a C program
to implements it.
4. What is a Data Structure? Explain in detail.
5. Explain infix, prefix & postfix expressions with examples.
6. Show how to implement a queue of integers in C by using an array int
q[QUEUESIZE] , where q[0] is used to indicate the front of the queue , q[1]
is used to indicate its rear and where q[2] through q[QUEUESIZE -1] contain
elements on the queue. Show how to initialize such an array to represent the
empty queue and write routines remove, insert and empty for such an
implementation.
7. What do you mean by stack? Explain with example.
8. Write a C program to evaluate Postfix Expression.
9. Write about circular queues and its implementation.
10. What are the applications of stack. Explain in detail.
References