Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Programming and Data Structures (R20 Syllabus JNTUA, Anantapuramu)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

C Programming and Data Structures

(R20 Syllabus JNTUA,Anantapuramu)


UNIT-III
Syllabus
Data Structures, Overview of data structures, stacks and queues, representation of a stack, stack related terms,
operations on a stack, implementation of a stack, evaluation of arithmetic expressions, infix, prefix, and postfix
notations, evaluation of postfix expression, conversion of expression from infix to postfix, recursion, queues -
various positions of queue, representation of queue, insertion, deletion, searching operations.

Overview of data structures


Data structure is a method of representation of logical relationships between
individual data elements related to the solution of a given problem.
Types of data structure
linear datastructure: In the linear data structure , values are arranged in a linear fashion. An array, linked
list,stacks and queues are examples of linear data structure in which values are stored in sequence.
Non-linear data structure: This type is opposite to linear. The data values in this structure are not arranged in
order. Trees,graph ,table and sets are examples of non-linear data structure.

Data structure

Linear Data structure None Linear Data structure

Arrays linked list stacks queues Trees Graphs tables sets

Fig: Types of data structure


Homogenous: In this type of data structure , values of same data types are stored. An example of it can be an
array that stores similar data type elements having different locations for each element.
Non Homogenous: In this type of data structure, data values of different types are grouped like in structure and
classes.
Dynamic:In dynamic data structures such as references and pointers, size and memory locations can be changed
during program execution
Static:A static keyword in c/C++ is used to intitialize the variable to 0(NULL).Static variable values remains in
the memory throughout the program.
stacks and queues
Stack is an ordered collection of elements which insertions and deletions takes place from one end only.
Queue is an ordered collection of elements which insertions takes place from one end that is rear end and
deletions takes place from other end that is front end.
Representation of Stack: one of the most important linear data structure is stack. A Stack is defined as an
ordered collection of data items in which insertions and deletions takes place from one end, called top of the
stack.Stack maintains a pointer called top which keeps track of the top most element in the stack. Any
insertions or deletions should be based on the value of top. It works on the basis of LIFO(Last-in-first-out)
According to the definition, new elements are inserted from top and the elements
are deleted from same end , i.e again top. This suggests that the element inserted most recently can be deleted.
In the stack, the elements are removed in the reverse order of that in which they were
added to the stack. Last element inserted is to be deleted first so it is called last in first out.(LIFO).
Example:The way the books are arranged on the table. The plates arranged on the table. Elements are inserted in
the order as A,B,C,D,E.
TOP E
D
C
B
A

It represents the stack of 5 elements


The top most element in the stack is E.
If we want to delete element, only E has to be deleted first (because it is LIFO).
If deletion operation is performed on stack continuously then the order in which elements are deleted is
E,D,C,B,A.
Stack related terms:
Top: The top of the stack indicates its door.The stack top is used to veify stacks current position that is
underflow or overflow.
Stack underflow:When there is no element in the stack or the stack holds elements less than its capacity then
this status of the stack is known as stack underflow.
Stack overflow:When the stack contains elements equal to its capacity and no more elements can be added ,such
status of the stack is known as stack overflow.In sucha position, the top rests at the highest position
Push:Inserting or adding element into the stack is called push.
Pop: Deleting or removing element from the stack is called pop.
operations on a stack: Consider a stack of maximum size 4 when top reaches max-1(3) stack is overflow.
Implementation of Stack:

Write a program to implementation of stack using arrays.

#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.,

Arithmetic Expression provides mainly three types of notations like


1. INFIX : In this, operator is placed between the operands.
2. PREFIX : In this, operator is placed before the operands.
3. POSTFIX : In this, operator is placed after the operands.

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

conversion of expression from infix to postfix


Let Q be an arithmetic expression written in Infix notation. Both side of operands and operators contain left and
right parentheses. We assume that Q contain exponentiations ( ^ ), multiplications ( * ), divisions ( / ), additions
( + ), subtractions ( - ). All the operations are performed based on polish rules.
The following algorithm transforms the infix expression Q into its equivalent postfix expression P. This
algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be
constructed from left to right using the operands from Q and the operators which are removed from STACK.
We begin by pushing a left parenthesis onto STACK and adding a right parenthesis at the end of Q. The
algorithm is completed when STACK is empty.
Algorithm : Infix_to_Postfix( Q, P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent
postfix expression P.
Step 1: Push “(“ onto STACK, and add “)” to the end of Q.
Step 2: Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the STACK
is empty.
Step 3: If an operand is encountered, add it to P.
Step 4: If left parenthesis encountered, Push it onto STACK.
Step 5: If an operator is encountered, then Repeatedly pop from STACK and add to P each
operator which has same.
[ End of If structure ]
Step 6: If a right parenthesis is encountered then a)Repeatedly pop from STACK and add to P
each operator until a left parenthesis is encountered. which has same.
b) Remove the left parenthesis.
[ End of If structure ]
[ End of step 2 loop ]

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

You might also like