DS-1 Removed
DS-1 Removed
MODULE I
7 0 0 0 0
0 0 9 0 0
3 0 2 0 0
0 0 0 0 5
8. Define Pointers. How to declare and initialize pointers, explain with example(5)
9. Define stack. List and explain the various operations on stacks using arrays with stack
overflow and stack underflow conditions (10)
10. Write a c program to perform push(), pop(), display operations on STACK (10)
11. Develop a C recursive program for Tower of Hanoi problem. Trace it for 3 disks with
schematic call tree diagram (8)
12. Write an algorithm to convert an infix expression to postfix expression and also trace the
same for the expression (a+b)*d+e/f+c
13. Write an algorithm to evaluate postfix expression and apply the same for the given
postfix expression. ABC-D*+E$F+ and assume A=6 B=3 C=2 D=5 E=1 F=7
14. Convert the following infix expression into postfix expression using stack (5)
A + (B*C-(D/E^F)*G)*H
1. Define data structure. Explain various operations on data structure (6)
Definition
“Data structure is defined as the way of storing and organizing data elements by referring its
relationship to each other for efficient usage”
2. Define data structure. Explain the classification of data structure with examples (6)
Definition
“Data structure is defined as the way of storing and organizing data elements by referring its
relationship to each other for efficient usage”
Classification Of Data Structures
Data structures are generally categorized into two classes:
Primitive data structures
Non-primitive data structures
Primitive data structure
The basic data types supported by all programming languages are called Primitive Data
Structure.
These data structures are directly operated upon by machine level instructions
Used to represent single values
Example: integer, float, double, character, String and Boolean.
Non-primitive data structure
The data structures which are derived by primitive data structures are called Non-
Primitive Data structures.
It is used to store group of values
Example: Arrays, Structure, Union, linked lists, stacks, Queue, trees, and graphs.
Non-primitive data structures can further be classified into two categories:
o Linear data structure
o Non-linear data structure
Linear data structure
If the elements of a data structure are stored in a linear or sequential order, then it is a
linear data structure.
It establishes the adjacency relationship between the elements
Two different Representations
o Sequential Memory Representation
Represents linear relationship between elements by means of sequential
memory locations
Example: Arrays, Queue, Stack
o Linked or Pointer Representation
Represents linear relationship between elements by means of links or
pointer.
Example: Linked List
Example: Arrays, linked lists, stacks, and queues.
Non-Linear data structure
If the elements of a data structure are stored based on the Hierarchical relationship
between the data, then it is called non-linear data structure.
Here adjacency relationship is not maintained between the elements.
Example: trees and graphs
3. Define structure. Explain the types of structures with examples for each and give
difference between union and structure (10)
Structure
Structure is a user defined data type used to represent a group of data items of different types
using a single name
Structure Declaration
A structure is declared using the keyword struct followed by the structure name
All the variables of the structure are declared within the structure
Each variable name declared within a structure is called a member of the structure
struct structure_name
{
type element 1;
type element 2;
……………..
type element n;
};
Declaration of Structure Variable
struct structure_name var1,var2,…..,var n;
Example
struct student
{
int rollno;
char name[25];
float totalmark;
};
struct student std1;
Declaration of structure with structure variable
It is possible to combine the declaration of structure combination with that of the structure
variables, as shown below.
struct structure_name
{
type element 1;
type element 2;
……………..
type element n;
} var1,var2,…,varn;
Example
struct student
{
int rollno;
char name[25];
float totalmark;
} std1, std2;
Types of Structure
i) Array of structures:
It is possible to store a structure has an array element. i.e., an array in which each element
is a structure. Just as arrays of any basic type of variable are allowed, so are arrays of a
given type of structure
Syntax
struct struct_name
{
type element 1;
type element 2;
……………..
type element n;
}array name[size];
Example:
struct student
{
int rollno;
char name[25];
float totalmark;
} stud[100];
ii) Structure within a structure or Nested Structure
Nested structure in C is nothing but structure within structure
One structure can be declared inside other structure as we declare structure members
inside a structure
Also called Embedded structure
Example
struct dob
{
int day;
int month;
int year;
};
struct student
{
struct dob d;
int rollno;
char name[25];
int totalmark;
}stud[25];
To access the data member
stud[i].d.day
stud[i].d.month
stud[i].d.year
4. List and explain the functions supported in C for dynamic memory allocation (7)
There are a number of operations that can be performed on arrays. These operations include:
Inserting an element in an array
Searching an element in an array
Deleting an element from an array
Traversing an array
Merging two arrays
Sorting an array in ascending or descending order
C Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100],key;
int element,i,loc,size,n=0,j=0;
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter %d array elements\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("List before Insertion: ");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to insert\n");
scanf("%d",&element);
printf("Enter a position to insert an element %d\n",element);
scanf("%d",&loc);
loc--;
for(i=size-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=element;
printf("\nDisplay: List after Insertion: ");
for(i=0;i<size+1;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to delete\n");
scanf("%d",&n);
i=0;
for(i=0;i<size;i++)
{
if(a[i]==n)
{
for(j=i;j<(size-1);j++)
{
a[j]=a[j+1];
}
break;
}
}
printf("Disply: List after deletion\n");
for(i=0;i<(size-1);i++)
{
printf("%d",a[i]);
}
printf("\nSearch");
printf("\nEnter the element to search\n");
scanf("%d",&key);
for(i=0;i<size;i++)
{
if(a[i]==key)
printf("key found");
}
return 0;
}
6. Explain any four string handling functions supported by c with syntax and example (6)
4) strrev (string)
strrev function accepts single string as parameter and reverse that string
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string[]="goal";
strrev(string);
printf("reverse string is %s",string);
}
Output: reverse string is laog
7. What is a Sparse matrix? Write the ADT of sparse matrix. Give the triple form of a
given matrix and also find its transpose. (8)
7 0 0 0 0
0 0 9 0 0
3 0 2 0 0
0 0 0 0 5
Sparse matrices are those matrices that have the majority of their elements equal to zero. In other
words, the sparse matrix can be defined as the matrix that has a greater number of zero elements than
the non-zero elements.
Transpose of a Matrix
7 0 3 0 5 4 5
0 0 0 0 0 0 7
0 2 3
0 9 2 0 2 1 9
0 0 0 0 2 2 2
4 3 5
0 0 0 5
8. Define Pointers. How to declare and initialize pointers, explain with example(5)
Pointer
o The pointer in C language is a variable which stores the address of another variable
o This variable can be of type int, char, array, function, or any other pointer
Pointer variable Declaration
Pointers must be declared before they can be used, just like a normal variable
The syntax of declaring a pointer is to place a * in front of the name
A pointer variable is associated with data type too
Synatx
data_type *ptr;
Example
int *p;
char *pch;
float *psal;
Pointer Variable Initialization
Initialization of a pointer variable refers to the process of assigning an address to the
pointer variable
Variable should be declared before initialization
Syntax
datatype *ptrvar = exp;
Example
int n;
int *v = &n;
* symbol indicates the pointer variable
Example Program
#include<stdio.h>
void main()
{
int a =30, *ptr1;
ptr1 = &a;
printf(“Value of a is %d\n”,*ptr1);
*ptr1 = 100;
printf(“Value of a after initialization is %d\n”,*ptr1);
return;
}
Output
Value of a is 30
Value of a after initialization is 100
9. Define stack. List and explain the various operations on stacks using arrays with
stack overflow and stack underflow conditions (10)
DEFINITION
A stack is a linear data structure with the restriction that insertions or deletions are limited
at one end called the top of the stack.
Example
Stack of coins
Bread arranged on a pile
Stack of plates
Stack Implementation
Array Representation
Stack can be represented as a linear array where elements are stored in sequential
memory location
Array representation of the stack is also called Static Implementation
It needs prior knowledge of maximum number of elements required to be processed
The integer variable Top which will keep track of the top of the stack as more and more
elements are inserted and deleted from the stack
Two variables associated with array implementation of stack
o Top
Retrieves topmost element of the stack
Top position is where the elements can be pushed or popped
If top=null, then stack is empty
If Top=Max-1, then stack is full
o Max
Maximum size of the stack
Operations
The primitive operations performed on the stack are
o PUSH
o POP
o STACK TOP
PUSH
o Push operation is used to insert a new element into the stack
o Push always inserts new element at the top of the stack
o After insertion the top is incremented by one and the new element becomes the top
of the stack
o Always check whether the stack is full before insertion, otherwise push operation
results in overflow state and the element cannot be added.
o Algorithm
Step 1: IF TOP = MAX-1
PRINT OVERFLOW
Go to step 4
[END OF IF]
Step 2: SET TOP = TOP + 1
Step 3: SET STACK[TOP] = VALUE
Step 4: END
o POP
o Pop operation delete an element from the top of the stack
o After every pop operation the top of the stack is decremented by one.
o Always check whether the stack is empty, otherwise pop operation results in stack
underflow state.
o Algorithm
Step 1: IF TOP = NULL
PRINT UNDERFLOW
Go to step 4
[END OF IF]
Step 2: SET VAL = STACK[TOP]
Step 3: SET TOP = TOP - 1
Step 4: END
o STACK TOP
o Stack top returns the value of the element at the top of the stack
o It does not delete the top element from the stack
o Stack top also results in underflow if the stack is empty
o Algorithm
Step 1: IF TOP = NULL
PRINT STACK IS EMPTY
Goto Step 3
Step 2: RETURN STACK[TOP]
Step 3: END
10. Write a c program to perform push(), pop(), display operations on STACK (10)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 3 // Altering this value changes size of stack created
int st[MAX], top=-1;
void push(int st[], int val);
int pop(int st[]);
int peek(int st[]);
void display(int st[]);
int main(int argc, char *argv[]) {
int val, option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st, val);
break;
case 2:
val = pop(st);
if(val != -1)
printf("\n The value deleted from stack is: %d", val);
break;
case 3:
val = peek(st);
if(val != -1)
printf("\n The value stored at top of stack is: %d", val);
break;
case 4:
display(st);
break;
}
}while(option != 5);
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
int pop(int st[])
{
int val;
if(top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val = st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n"); // Added for formatting purposes
}
}
int peek(int st[])
{
if(top == -1)
{
printf("\n STACK IS EMPTY");
return -1;
}
else
return (st[top]);
}
Output
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option : 1
Enter the number to be pushed on stack : 500
11. Develop a C recursive program for Tower of Hanoi problem. Trace it for 3 disks
with schematic call tree diagram (8)
int main()
{
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
12. Write an algorithm to convert an infix expression to postfix expression and also
trace the same for the expression (a+b)*d+e/f+c
(a+b)*d+e/f+c ab+d*ef/+c+
13. Write an algorithm to evaluate postfix expression and apply the same for the given
postfix expression. ABC-D*+E$F+ and assume A=6 B=3 C=2 D=5 E=1 F=7
Evaluation of a Postfix Expression
The ease of evaluation acts as the driving force for computers to translate an infix notation
into a postfix notation.
That is, given an algebraic expression written in infix notation, the computer first converts
the expression into the equivalent postfix notation and then evaluates the postfix
expression.
Both these tasks—converting the infix notation into postfix notation and evaluating the
postfix expression—make extensive use of stacks as the primary tool.
Using stacks, any postfix expression can be evaluated very easily.
Every character of the postfix expression is scanned from left to right.
o If the character encountered is an operand, it is pushed on to the stack.
o If an operator is encountered, then the top two values are popped from the stack
and the operator is applied on these values.
o The result is then pushed on to the stack.
Algorithm
14. Convert the following infix expression into postfix expression using stack (5)
A + (B*C-(D/E^F)*G)*H