0% found this document useful (0 votes)
8 views

Define Data Structures

Data structures

Uploaded by

gvck8wv8bp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Define Data Structures

Data structures

Uploaded by

gvck8wv8bp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DSA QB ANSWERES

1. Define Data Structures. Explain the classification of data structures with examples.
What are the Primitive operations that can be performed. (6marks)
ANS

Definition: The study of


• how the data can be collected and stored in main memory during execution
• how the data can be represented,

• how the data is organized or how the data is categorized

how efficiently the data can be retrieved and manipulated



and the possible ways in which different data items are logically related is called data

h
structure. The data structures are classified into :
Primitive data structures

Non-primitive data structures

Primitive data structures


Definition: The data structures that can be manipulated directly by machine
instructions are called primitive data
structures. The primitive data structures are fundamental data types that are
supported by any programming language. For example,
■ integers ( int)
■ floating point numbers ( float)
■ characters ( char )
■ double values ( double )
■ pointers
are all primitive data structures in C language.

Non-primitive data structures :

Definition: The data structures that cannot be manipulated directly by machine


instructions are called non-primitive data structures. The non-primitive data
structures are created or constructed using primitive data structures.
For examples,

■ arrays
■ stacks
■ queues
■ linked lists
■ trees
are all non-primitive data structures in C language.

The various operations performed on data structures are:


■ Traversing
■ Inserting
■ Deleting
■ Searching
■ Sorting

2. Write a C program to demonstrate the basic operations on arrays.(8 marks)
ANS:

#include <stdio.h>
#include<stdlib.h>
#define MAX_SIZE 5 // Maximum size of the array
//display function
void display(int a[], int n)

{
Int i ;

for (int i = 0; i < n; i++)


{

printf("%d ", a[i]);


}
printf("\n");
}
//insert function

void insert( int item, int a[] , int n, int pos)


{
Int i ;
if (n == MAX_SIZE)
{
printf("Array is full.\n");

return n;
}

if (pos > n || pos < 0)


{

printf("Invalid position. \n",);


return n;
}

for ( i = n-1; i > pos; i--)

{
a[i+1] = a[i ];
}
a[pos] = item;
return n+1;

}
// delete function
void delete(int a[], int n, int pos)
{
Int i ;

if (n == 0)
{
printf("Array is empty.\n");
return n;
}
if (pos < 0 || pos >= n)
{
printf("Invalid position.);

return n;
}

for (int i = pos; i < = n - 1; i++)


{

a[i-1] = a[i ];
}
n --;
}

void main()
{
int a[10], n = 0, choice, element, position;
printf(“1. Insert 2. Delete 3. Display 4.Exit ”);
scanf(“%d”,&choice);

switch (choice)
{
case 1:
printf("Enter item: ");
scanf("%d", &item);

n = insert(item, a , n, pos);
break;

case 2:
printf("Enter position to delete: ", );
scanf("%d", &pos);
n = delete(a, n, pos);
break;
case 3:

printf(“Array :”);
display(a , n);
break;
default:
exit(0);

}
}

3. Explain in brief, the different memory management functions available in C for dynamic
memory allocation.(8 marks)
ANS :
The various memory management function available in C are :
■ malloc()
■ calloc()
■ realloc()

malloc():
Definition: malloc0 is the shorthand name for memory allocation which is a built in
function in C. It is declared in the header file "stdlib.h". Using this function the
programmer can request the Operating System to allocate a block of contiguous
memory according to the size specified in the argument

■ The required memory space for the data is allocated during run-time in heap area.
■ If specified memory space is not available, the function malloc() returns NULL.
■ If memory space is successfully allocated, the function malloc0
returns address of the first byte. Syntax:
#include <stdlib.h>
void *malloc ( size_t size)

Calloc():
Calloc() is the shorthand name for memory allocation which is a built in function in C. It
is declared in the header file "stdlib.h".
#include <stdlib.h> data_type *Ptr;
ptr = ( dat_type * ) calloc( n, size); Takes two arguments:
n: number of blocks to be allocated
size: number of bytes to be allocated
Realloc():
Definition: realloc() is a built in function in C using which the programmer can resize
the memory space which is already allocated using either malloc() or calloc()
functions.
■ It is declared in header file "stdlib.h"
■ Reallocating the memory space using realloc() without allocating the memory
using malloc() or callocO is not possible. Ifwe do so, we get unpredictable
results.
Syntax:
#include <stdlib.h>
void *realloc ( void *ptr, size_t size )

4. Convert the following expression from infix to postfix using stacks. (Any expression can be
asked) 4 marks

ANS
5.
6. Write a function to evaluate the postfix expression. Illustrate the same for the given
postfix expression using stacks. (Any expression can be asked) 8 marks
ANS
7. Develop a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with
REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR( 8 marks)

ANS
//replace

//main
8. Write a C program to implement push, pop and display operations for stacks using arrays.
6-8 marks
ANS
9. Develop C program to print the array elements in reverse order using recursion.
Ans
#include <stdio.h>

void printReverse(int arr[], int index)

{
// Base case: if index is less than 0, stop recursion
if (index < 0) {
return;
}

// Print the current element


printf("%d ", arr[index]);

// Recursive call to print the previous element

printReverse(arr, index - 1);


}
void main()
{
int n;

// Prompt user to enter the number of elements


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];

// Input elements in the array


printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)

{
scanf("%d", &arr[i]);
}

// Print array in reverse order using recursion

printf("Array in reverse order:\n");


printReverse(arr, n - 1);
}

10. Write a C function to insert an element at the rear end using Singly Linked List 4 marks
ANS
11. What are the disadvantages of ordinary queue? Discuss the implementation of circular
queue 8 marks
ANS
12. Define Polynomial. Explain how to represent a polynomial using array of structures.
Write a C program to read and display a polynomial. 8 marks
ANS
13. Define queue? Explain how to represent queue using Dynamic arrays. 4 marks
Ans
Definition : queue is a special type of data structure where elements are inserted from one
end and are deleted from another end.
14 . Give the structure definition for Singly Linked List (SLL). Write a C function to,
i. insert an element at the front end of SLL
ii. Delete a node at the beginning of SLL

Ans

Definition : a linked list is a data structure which is a collection of zero, one or more nodes
where each node is connected to next node by a link which contains address of the next
node

i. Insert an element at the front end of SLL


Ans
ii. Delete a node at the beginning of SLL
Ans

You might also like