C Programming and Data Structures (R20 Syllabus JNTUA, Anantapuramu)
C Programming and Data Structures (R20 Syllabus JNTUA, Anantapuramu)
C Programming and Data Structures (R20 Syllabus JNTUA, Anantapuramu)
Data structure
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void push(int);
void pop();
void display();
int stack[SIZE], top = -1;
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value){
if(top == SIZE-1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop(){
if(top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display(){
if(top == -1)
printf("\nStack is Empty!!!");
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}output: Menu
1.Push
2.Pop
3.display
Enter your choice 1
Enter the value to be insert:10
Insertion success!!!
Enter your choice 1
Enter the value to be insert:20
Insertion success!!!
Enter your choice 1
Enter the value to be insert:30
Insertion success!!!
Enter your choice 3
stack elements are
30
20
10
Enter your choice 2
Deleted:30
Enter your choice 3
stack elements are
20
10
Enter your choice 4
Evaluation of Arithmetic Expression
Polish implemented rules to perform the arithmetic notation using reverse notation, called postfix notation.
The rules are inner most parenthesis will be evaluated at first
Exponentiation will be performed in second step
Multiplication and division will be performed in step 3
Additions and Subtraction will be performed in step 4.
Arithmetic Expression: An expression is a combination of operators & operands. It contains arithmetic operators
called Arithmetic Expression. Eg: a+b, a+(b*c)/d etc.,
Generally, we write the expression in infix expression, those expression are automatically convert into
prefix or postfix expressions because to solve the expression very fast. Stacks are very useful to convert the infix
expression into prefix or postfix expressions.
Algorithm:
The infix expression is scanned from left to right.
When an operand is scanned then it is placed in postfix or prefix expression.
When an operator or left brace is scanned then it is placed in stack.
When right brace is scanned then pop the stack and append that operator in postfix or prefix expression.
Repeat the above steps until you reach the end of the expression.
Finally pop the stack until stack is empty.
Infix, prefix, and postfix notations
We use three kinds of notations to express arithmetic expressions. These three kinds of notations are
infix, prefix and postfix.
infix notation:Expressions are generally expressed with infix notation with binary operator between the
operands. The binary operator needs the operands. Following are examples of infix notations.
1.x+y
2.x+y*z
3.(x+y)*z
4.(x+y)*(p+q)
Prefix notation: The prefix notation is also called as polish notation. The polish mathematician invented it.In
this type, the operator precedes the operands. The following are the examples of prefix notation.
1.+xy
2.+x*yz
3.*+xyz
4.*+xy+pq
postfix notation: the postfix notation is also called as reverse polish notation. The operator follows the operands.
The following are the examples of postfix notation
1.xy+
2.xyz*+
3.xy+z*
4.xy+pq*+
Evaluation of postfix expression:
The postfix expressions are very simple. On the other hand, finding postfix expressions with
a program is complicated. This is because the numbers and characters are mixed in one string. The programmer needs
initially to separate digits and symbols. After this , process of arithmetic operations is performed.
Each operator in the postfix expression is associated with the previous two operands. When we
read an operand from a given expression, it is pushed onto the stack. When an operator symbol is read, the operands
associated with it are the top elements of the stack. We can pop these two elements from the stack and perform the
operation. The obtained result from the last operation is again pushed onto the stack.
INFIX = A + ( B*C )/D POSTFIX = ABC*D/+
= 2 + ( 2*2) / 2 =222*2/+
= 2 + 4/2 =242/+
=2+2 =22+
=4 =4
Queues:
A queue is special type of data structure in which insertions take place from one end called rear end and deletions
takes place from other end called front end. Queue works on the basis of first in first out(FIFO). since the first
element entered in a queue will be the first element to be deleted. This type of data structure is used in time
sharing systems where many user jobs will be waiting in the system queue for processing. These jobs may
request the services of CPU, main memory or external devices such as printer.
A Queue can be represented using sequential allocation(using arrays)
Various positions of queues
Representation of Queues:
The queue can be put into operation in two ways
a)static implementation(array)
b)dynamic implementation(pointers)
static implementation (array): The static implementation means an array implementation. In this
implementation, we must know the number of elements to be kept in the queue. The size of the array is
decided before execution at compile time. Once the array is declared, its size can not be altered.
Write a program to implement a queue using an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int queue[8];
int rear=0;
clrscr();
while(1)
{
printf(“\n enter element \n”);
scanf(“%d”,&queue[rear]);
rear++;
if(rear==7)
{
printf(“\n queue is full \n”);
break;
}
}
printf(“\n Elements of queue are \n”);
rear = 0;
while(1)
{
printf(“ %d”, queue[rear]);
rear++;
if(rear==7)
break;
}
}
output:
Enter element: 1
Enter element:2
Enter element:3
Enter element:4
Enter element:5
Enter element:6
Enter element:7
Queue is full
Elements of Queue are: 1 2 3 4 5 6 7
Dynamic implementation is carried out using pointers.By applying increment, operation on pointer successive
locations of memory can be accessed and an element can be stored in that location. Thus, the series of elements
can be continued to any number of elements. The programmer has to keep the starting address of the pointer in
which the first element is stored. Thus in the same way the numbers can be viewed or altered, here is the simple
example supporting the above concept.
Write a program to implement queue-using pointers.
#include<stdio.h>
#include<conio.h>
int main()
{
int *q;
int rear=0;
clrscr();
while(1)
{
printf(“\n Enter element: “);
scanf(“%d”,q);
q++;
rear++;
if(rear ==7)
{
printf(“\n queue is full “);
break;
}
}
q = q-rear;
printf(“\n elements of queue are \n”);
rear = 0;
while(rear <7)
{
printf(“\n element:%d read = %d”,*(q+rear),rear);
rear++;
}
return(0);
}
output:
Enter element:4
Enter element:2
Enter element:3
Enter element:4
Enter element:7
Enter element:8
Enter element:9
Queue is full
Elements of queue are:
element:4 rear = 0
element:2 rear = 1
element:3 rear =2
element:4 rear = 3
element:7 rear = 4
element:8 rear = 5
element:9 rear = 6
Queue Insertion, deletion and searching operations program:
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void enQueue(int);
void deQueue();
void search(int);
int queue[SIZE], front = -1, rear = -1;
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. search\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: printf(“\n Enter the value to search \n”);
scanf(“%d”,&value);
search(value);
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void search(value){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue element found at :\n");
for(i=front; i<=rear; i++)
if(value==queue[i])
printf("%d position \t",++i);
}
}
output:
MENU
1.Insertion
2.Deletion
3.Search
4.Exit
Enter your choice1
Enter the value to be insert:10
Insertion success
MENU
1.Insertion
2.Deletion
3.Search
4.Exit
Enter your choice1
Enter the value to be insert:20
Insertion success
MENU
1.Insertion
2.Deletion
3.Search
4.Exit
Enter your choice1
Enter the value to be insert:30
Insertion is success
MENU
1.Insertion
2.Deletion
3.Search
4.Exit
Enter your choice3
Enter the value to search
30
Queue element found at :
3 position
MENU
1.Insertion
2.Deletion
3.Search
4.Exit
Enter your choice 2
Deleted:10
MENU
1.Insertion
2.Deletion
3.Search
4.Exit
Enter your choice 4