M1 - Final To Host
M1 - Final To Host
Data Structure:
3 compiler design
stacks are used in pushing
compilers syntax check for matching braces .implemented by stack
4. Software development
Data Structures are…
Array
Stack
Queue
Linked List
Tree
Graph
File
Explain classification
of data structure
Or
Lists different types of
data structure with an ex
Primitive data structure
The data structures, that are directly operated
upon by machine level instructions i.e. the
fundamental data types such as
➢ int,
➢float,
➢ char
➢Pointer
Define non-premitive data
structure. Give example
A data structure that cannot be
manipulated directly by machine
instructions are called non-premitive data
structure .
int aitm[5] 10 20 30 40 50
➢One-dimensional arrays
➢Multidimensional arrays
Array Elements
or
Items
Stacks
A stack is a linear data structure in which an element
may be inserted or deleted only at one end called the
top end of the stack
→last-in-first-out (LIFO)
Application of Stacks
➢Recursive functions
➢Reversing string
➢Evaluation of Expression
➢Conversions of expressions infix, postfix, prefix
➢Implementation of function calls
Queue
Queue is a linear data structure in which insertion can take
place at only one end called rear end and deletion can take
place at other end called front end.
The front and rear are two terms used to represent the two
ends of the list when it is implemented as queue.
10 20 30 40
Data link
Trees
A tree is a nonlinear data structure and is
generally defined as a nonempty finite set of
elements, called nodes such that:
1. It contains a distinguished node called root of
the tree.
2. The remaining elements of tree is called child
nodes
A graph normally a combination of the set
of vertices V and set of edges E.
Difference between structure and union
struct keyword union keyword
unique memory location memory location is shared
Changing the value of one Changing the value of one
data member will not affect data member will affect
other data members other data members
we can retrieve any member we can retrieve only one
at a time member at a time
SELF-REFERENTIAL STRUCTURES
}
Define pointers. Give
advantages and
disadvantages of pointers
What are the Applications of pointer?
1)Allocate and access memory dynamically
2) Return more than one value from a function
3)Create and manipulate data structure such as linked
lists, trees,
4) Passing of arrays and strings from one function to
another with greater flexibility.
Disadvantages of pointers:-
Pointers are slower than normal
variables.
If pointers are updated with incorrect
values, it might lead to memory corruption.
we can access the restricted memory
area.
if sufficient memory is not available
during runtime for the storage of pointers,
the program may crash
Dynamic Memory allocation
Dynamic Memory allocation
Dynamic memory allocation is the process of
allocating memory during run time(execution time).
Additional storage can be allocated whenever
needed.
List and explain any 4 dynamic
memory functions with an
examples
To implementing dynamic memory the following 4
functions are used.
1.malloc( ):
2. calloc( ):
3. realloc( ):
4. free( ):
1. malloc( ):
Allocates requested number of bytes and returns a
pointer to the first byte of the allocated space.
Syntax:
ptr=(datatype *)malloc(size);
where,
ptr is a pointer variable of type datatype
datatpe can be any of the basic datatype or user define datatype
Size is number of bytes required.
Example:
int *p;
ptr=(int*)malloc(100*sizeof(int));
It will allocate either 200 or 400 bytes size of the int is either 2 or 4 bytes
The calloc Function
It stands for contiguous allocation. It is used to allocate
multiple blocks of memory.
It requires two parameters as number of elements and size
of each element.
Syntax:
ptr=(datatype *)calloc(n , sizeof(Datatype);
where,
ptr is a pointer variable of type datatype
datatype can be any of the basic datatype
n is number of blocks to be allocated
size is number of bytes required.
realloc()
It changes the size of block by deleting or
extending the memory at end of the block.
If memory is not available it gives complete
new block.
Syntax:
ptr=(datatype *)realloc (ptr , sizeof(datatype);
where,
ptr is a pointer to a block previously allocated memory
either using malloc() or calloc()
Size is new size of the block.
free()
This function is used to de-allocate(or
free) the allocated block of memory
which is allocated by using functions
malloc(), calloc(), realloc().
Syntax:
free(ptr);
ARRAYS
Definition
An Array is defined as, an ordered set of similar data items. All
the data items of an array are stored in consecutive memory
locations.
Abstract Data Type:
Array create:
Item retrive:
Array store:
Declaration of one-dimensional array
data_type array_name [array_size] ;
#include<stdio.h>
String null
Integer compare(s,t)
Boolean isnull(s)
Integer Length(s)
String concat(s,t)
String Substr(s,I,j)
PATTERN MATCHING ALGORITHMS
Write knuth morris pratt pattern
matching algo and apply the same
to search the
pattern ‘abcdabcy’ in the
text ‘abcxabcdabxabcdabcdabcy’
Or write 2nd lab program
Knuth morris pratt pattern matching algorithm
Ex : 2
2x +3x+1
The largest (or leading) exponent of a
polynomial is called its degree.
Consider two polynomials show
pictorial representation in 1 d
array also give c representation
poly_pointer padd(poly_pointer a, poly_pointer b)
{
poly_pointer c, rear, temp;
int sum;
function to add
rear =(poly_pointer)malloc(sizeof(poly_node));
front = rear;
two polynomials
while (a && b)
{
switch (COMPARE(a->expon, b->expon))
{
case -1: /* a->expo n < b->exp on */
attach( b->coef, b -> expon, &r ear);
b= b->link; br eak;
case 0: / * a->exp on == b->expon */
sum = a ->coef + b-> coef;
if (sum) att a c h (sum ,a->expon,&rear);
a = a->link; b = b->link; break;
case 1: /* a->expon > b->expon */
atta ch(a->coef, a->expon, &rear);
a = a->link;
}
Give ADT of sparse matrix and show
with suitable example sparse matrix
representation storing as triplets give
sample transpose functions
SPARSE MATRICES
Sparse matrix is a special matrix made of
m rows and n columns, therefore having
total m x n values with most of its elements
are zero.
We can also assume that if (m * n) / 2
elements are zero then it is a sparse matrix.
ADT SparseMatrix
A set of triples, <row, col, value>,
functions
void create(n): creates a SparseMatrix
SparseMatrix transpose(A)
SparseMatrix add(A,B):
SparseMatrix multiply(A,B)
ADT SparseMatrix
A set of triples, <row, col, value>,
functions
void create(n): creates a SparseMatrix that can hold n non-zero elements
information.
C
B B
A A A
Element stack[MAX_SSTACK_SIZE];
Int top = -1;
Here, the DATA field is for the ITEM, and the LINK field is, as usual,
to point to the next' item.
DATA
Write the algorithm to implement a
stack using dynamic array whose initial
capacity is 1 and array doubling is used
to increase the stack’s capacity(ie
dynamically reallocate twice the
memory) whenever an element is added
to full stack. Implement the operations
push pop and display (8)
Stack using Dynamic Arrays
Concept
whenever the inside of the stack is full, the memory is
expanded by dynamic allocation of the realloc
function.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int key;
} element;
void StackFull()
{
REALLOC(stack, 2*capacity*sizeof(*stack);
capacity *= 2;
}
String Reversal
Stack Applications:
-Polish notation,
-Infix to postfix conversion,
-Evaluation of postfix expression.
Polish notation
Polish Notation is a way of expressing
arithmetic expressions that avoids the use of
brackets to define priorities for evaluation of
operators
Example:
Infix notation with parenthesis: (a +b)
operator : + +, *, -
operands : A,B A,B,C,D
Types of expressions
Infix expression
Prefix expression
Postfix expression
Types of expressions
Infix expression: the operator is place
in between 2 operands
(A+B) * (C-D)
Prefix: the operator is place before the
operands
+AB
Postfix: the operator is place after its
operands
A B+
Convert the infix to postfix
((a/(b-c+d))*(e-a)*c) .
Write the postfix form of the following.
((6+(3-2)*4)!^5+7) (4)
A$b$c*D (4)
(a+b)*d+e/(f+a*d)+c
((a/(b-c+d))*(e-a)*c)
Postfix Expression
It follows the scheme of
<operand> <operand> <operator>
E.g. AB+
To make evaluation of an expression we should follow
the operator precedence table.
($ or ↑ or ^) Priority is 4
/ * Priority is 3
+– Priority is 2
# Priority is 1
Note:
$/ ^ represents exponentiation
23
Ex : 2$3$2 : 2 =512
3 2 =9
2 9 =512
1 Draw the table along with these headings characters,
stack and Result.
characters stack result
4.If the operator’s precedence is less than the precedence of the operator
then “pop out an operator from the stack and add it to the postfix result
4. If the character is “)”, then “pop out an operator from the stack and
add it to the postfix result.
Examples :->
1) A+(B*C-(D/E^F)*G)*H
2) ((A/B(B-C+D))*(E-A)*C
3) A/B-C+D*E-A*C
4) A*B/C
5) 2+3*4
6) A*B+5
7) ((((A/B)-C)+(D*E))-A*C))
8) A*(B+C)*D
9) A*B*C
10)A+B-C+D
11)A*-B+C
12)(A+B)*D+E/(F+A*D)+C
Evaluation of postfix expression.
Scan the postfix expression from left to right.
If the scanned symbol is an operand, then push it
onto the stack.
A Top element
B Next to top element
Make operation
B (operator) A
<B> <operator> <A> respectively. Perform operation and
push onto stack
Ans : 36
A
B
Ex2 :
10 2 8 * + 3 -
Ans 23
FUNCTION to evaluate postfix expression (4 times asked )
int eval(void)
{ Switch(token)
precedence token; {
Char symbol; case + : push(op1+op2); break;
int op1, op2; case - : push(op1-op2); break;
int n=0; case * : push(op1*op2); break;
int top=-1; case / : push(op1/op2); break;
token =gettoken(&symbol,&n); Case % : push(op1%op2); break;
While(token!=eos) }
{ }
if(token == operand) Token=gettoken(&symbol,&n);
push(symbol) }
else Return pop();
{ }
op2=pop();
op1=pop();
Recursion –
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi, is a mathematical puzzle which
consists of three towers and more than one disks
is as follows −
These disks are of different sizes and stacked upon in
an ascending order, i.e. the smaller one sits over the
larger one.
Rules
The mission is to move all the disks to some another
tower without violating the sequence of arrangement.
Only one disk can be moved among the towers at
any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
Steps :--
void main()
{
int n;
printf("enter number of disks\n");
scanf("%d",&n);
towerhonai(n, 'A', 'C', 'B');
printf("Number of Moves Taken =%d\n",count);