Define Data Structures
Define Data Structures
1. Define Data Structures. Explain the classification of data structures with examples.
What are the Primitive operations that can be performed. (6marks)
ANS
h
structure. The data structures are classified into :
Primitive data structures
■ arrays
■ stacks
■ queues
■ linked lists
■ trees
are all non-primitive data structures in C language.
#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 ;
return n;
}
{
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;
}
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>
{
// Base case: if index is less than 0, stop recursion
if (index < 0) {
return;
}
int arr[n];
{
scanf("%d", &arr[i]);
}
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