Data Structure Using C | Data Structures and Algorithms | Learn C


---------------×------------------××--------------------×---------------

 Data Structure Using C 

---------------×------------------××--------------------×---------------




---------------×------------------××--------------------×---------------

Data Structure Using C Question and Answers.

---------------×------------------××--------------------×---------------

1.  Write any four operations that can be performed on data structure.

Ans.:- 

A. Data structure operations (Non Primitive)

1. Inserting: Adding a new data in the data structure is referred as
insertion.

2. Deleting: Removing a data from the data structure is referred as
deletion.

3. Sorting: Arranging the data in some logical order (ascending or
descending, numerically or alphabetically).

4. Searching: Finding the location of data within the data structure
which satisfy the searching condition.

5. Traversing: Accessing each data exactly once in the data structure
so that each data item is traversed or visited.

6. Merging: Combining the data of two different sorted files into a
single sorted file.

7. Copying: Copying the contents of one data structure to another.

8. Concatenation: Combining the data from two or more data
structure.

B. Data structure operations (Primitive)

1. Creation: To create new Data Structure

2. Destroy: To delete Data Structure

3. Selection: To access (select) data from the data structure

4. Updating: To edit or change the data within the data structure.

---------------×------------------××--------------------×---------------

2. Define the term overflow and underflow with respect to stack.

Ans.:- 

Stack overflow: When a stack is full and push operation is performed to
insert a new element, stack is said to be in overflow state.


Stack underflow: When there is no element in a stack (stack empty) and
pop operation is called then stack is said to underflow state.


---------------×------------------××--------------------×---------------

3. Define Data Structure 

Ans.:- Data Structure is mathematical or logical way of organizing data in memory where data is stored inside a given memory and their relationship is also considered.

---------------×------------------××--------------------×---------------

4. Explain need of Data Structure 

Ans.:- 1. Data Structure organises data more efficiently.
2. Data Structure makes code easier to understand.
3. Data Structure is a way of organizing all data items that considers not only the
elements stored but also their relationship to each other.
4. Data structure is a representation of the logical relationship existing
between individual elements of data.
5. Data Structure makes computer easier to run code fast.

---------------×------------------××--------------------×---------------

5. Primitive Data Structure

Ans.:- 1. Primitive data structures are basic structures and are directly operated upon by machine instructions.
2. Primitive data structures have different representations on different computers.
3. Integers, floats, character and pointers are examples of primitive data structures.
4. These data types are available in most programming languages as built in type.
  • Integer: It is a data type which allows all values without fraction part. We can use it for whole numbers.
  • Float: It is a data type which use for storing fractional numbers.
  • Character: It is a data type which is used for character values.
  • Pointer: A variable that holds memory address of another variable are called pointer.
---------------×------------------××--------------------×---------------

6. Non primitive Data Structure 

Ans.:- 
1. These are more sophisticated data structures.
2. These are derived from primitive data structures.
3. The non-primitive data structures emphasize on structuring of a group of homogeneous or heterogeneous data items.
4. Examples of Non-primitive data type are Array, List, and File etc.
5. A Non-primitive data type is further divided into Linear and Non-Linear data structure.
  • Array: An array is a fixed-size sequenced collection of elements of the same data type.
  • List: An ordered set containing variable number of elements is called as Lists.
  • File: A file is a collection of logically related information. It can be viewed as a large list of records consisting of various fields.
---------------×------------------××--------------------×---------------

7. Difference between Linear and Non Linear Data Structure

Ans.:- 




---------------×------------------××--------------------×---------------

8. What is Time Complexity?

Ans.:- The time complexity of an algorithm is basically the running time of a program as a
function of the input size.

---------------×------------------××--------------------×---------------

9. What is Space Complexity ?

Ans.:- Space complexity of an algorithm is the amount of computer memory that is required
during the program execution as a function of the input size.

---------------×------------------××--------------------×---------------

10. Operations on Data Structure 

Ans.:- Traversing, Insertion, Deletion, Sorting, Searching, Merging, etc.

---------------×------------------××--------------------×---------------

11. What is Time Complexity of Linear Search ?

Ans.:- Time Complexity of Linear Search is O(n).

---------------×------------------××--------------------×---------------

12. What is Time Complexity of Binary Search ?

Ans.:- Time Complexity of Binary Search is 
O(log n). At the most n/2 comparison will be required to search a data element in an array.

---------------×------------------××--------------------×---------------

13. What is Sorting ?

Ans.:- Sorting means arranging the elements of an array so that they are placed in some relevant
order which may be either ascending or descending.

---------------×------------------××--------------------×---------------

14. What is Searching ?

Ans.:- Searching is used to find the location where an element is available. There are two
types of search techniques. They are:
  • Linear or sequential search
  • Binary search
---------------×------------------××--------------------×---------------

15. Note on Linear Search 

Ans.:- This is the simplest of all searching techniques. In this technique, an ordered or
unordered list will be searched one by one from the beginning until the desired element is
found. If the desired element is found in the list then the search is successful otherwise
unsuccessful. Suppose there are n elements organized sequentially on a List. The number of
comparisons required to retrieve an element from the list, purely depends on where the
element is stored in the list. If it is the first element, one comparison will do; if it is second
element two comparisons are necessary and so on. On an average you need [(n+1)/2]
comparisons to search an element. If search is not successful, you would need ‗n’
comparisons.

---------------×------------------××--------------------×---------------

16. Algorithm of Linear Search 

Ans.:- Let x[n] be the array of size n and key is an element to be searched in array.
  1.  Repeat for i=0 to n-1
  2.       If x[i]==key 
  3.         Then print "Element is found at i+1"
  4. If i==n 
  5.   Then print "Element is not found."
---------------×------------------××--------------------×---------------

17. Algorithm of Binary Search 

Ans.:- Let X[n] be the array of size n and key is an element to be searched in array.
  • low is starting point of array
  • high is ending point of array
  • mid is middle point in an array
1. low=0 , high= n-1 , mid = (high+low)/2
2. Repeat step 3 and 4 while low <= high & 
    A[mid]!=key
3. If key > A[mid]
          Then low = mid + 1
    Else If key < A[mid]
           Then high = mid - 1
    Else If key == A[mid]
           Then print "Key is Found at mid + 1"
4. Set mid = (high+low)/2
5. If low > high 
            Then print "Key is not Found"

---------------×------------------××--------------------×---------------

18. Algorithm of Bubble Sort 

Ans.:- Let x[n] be the array of size n 
  1. Repeat for i=0 to (n-1)
  2.          Repeat for j=0 to (n-i-1)
  3.                If x[j] > x[j+1]
  4.                   Interchange x[j] with x[j+1]
  5. Print sorted array x[n]
---------------×------------------××--------------------×---------------

19. Algorithm of Insertion sort 

Ans.:- Let x[n] be the array of size n 
  1. Repeat for i=1 to n-1
  2.    Set temp=x[i] & j=i-1
  3.         Repeat while j>=0  & A[j]<temp do
  4.                 A[j+1]=A[j]
  5.                  j=j-1
  6.         A[j+1] = temp 
---------------×------------------××--------------------×---------------

20. Algorithm of Selection Sort

Ans.:- 



---------------×------------------××--------------------×---------------

21. Algorithm of Radix Sort 

Ans.:- 

---------------×------------------××--------------------×---------------

22. Example of Radix Sort 

Ans.:- 



---------------×------------------××--------------------×---------------

23. Write a Program to implement a stack with push, pop, and display operations.

Ans.:- 

#include <stdio.h>
#include <stdlib.h>

#define MAX 5 // Maximum size of the stack

// Stack structure definition
struct Stack 
{
    int items[MAX];
    int top;
};

// Initialize the stack
void init(struct Stack* stack) 
{
    stack->top = -1;
}

// Check if the stack is full
int isFull(struct Stack* stack) 
{
    return stack->top == MAX - 1;
}

// Check if the stack is empty
int isEmpty(struct Stack* stack) 
{
    return stack->top == -1;
}

// Push an item to the stack
void push(struct Stack* stack, int value)
{
    if (isFull(stack)) 
    {
        printf("Stack is full. Cannot push %d\n", value);
    } 
    else 
    {
        stack->items[++(stack->top)] = value;
        printf("%d pushed onto stack\n", value);
    }
}

// Pop an item from the stack
void pop(struct Stack* stack) 
{
    if (isEmpty(stack)) 
    {
        printf("Stack is empty. Nothing to pop\n");
    } 
    else 
    {
        printf("%d popped from stack\n", stack->items[(stack->top)--]);
    }
}

// Display all the elements of the stack
void display(struct Stack* stack)
 {
    if (isEmpty(stack)) 
   {
        printf("Stack is empty\n");
    }
   else
   {
        printf("Stack elements: ");
        for (int i = stack->top; i >= 0; i--) 
        {
            printf("%d ", stack->items[i]);
        }
        printf("\n");
    }
}

// Main function
int main() 
{
    struct Stack stack;
    int choice, value;

    init(&stack);

    do {
        printf("\nStack Operations:\n");
        printf("1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: ");
        scanf("%d", &choice);

        switch (choice) 
        {
            case 1:
                printf("Enter value to push: ");
                scanf("%d", &value);
                push(&stack, value);
                break;
            case 2:
                pop(&stack);
                break;
            case 3:
                display(&stack);
                break;
            case 4:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice! Please try again.\n");
        }
    } while (choice != 4);

    return 0;
}

---------------×------------------××--------------------×---------------

24. Write a C Program to Implement Linear Search.

Ans.:- 

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,key,n;
clrscr();
printf("\nEnter size of array:");
scanf("%d",&n);
printf("\nEnter array elements are:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter key element to be search:");
scanf("%d",&key);
for(i=0;i<n;i++)
{
    if(key==a[i])
    {
        printf("\n Element is found at %d",i+1);
        break;
    }
}
if(i==n)
{
    printf("\n Element is not found");
}
getch();
}

---------------×------------------××--------------------×---------------

25. Implement a c program to insert an element in an array.

Ans.:- 

#include <stdio.h>
void insert(int arr[], int n, int x, int pos) {
    if (pos > n + 1) {
        printf("Invalid position!\n");
        return;
    }
    // Shift elements to the right to make space for the new element
    for (int i = n - 1; i >= pos - 1; i--) {
        arr[i + 1] = arr[i];
    }
    // Insert the new element at the specified position
    arr[pos - 1] = x;
    n++;
    printf("Array after insertion: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}
int main() {
    int arr[100], n, x, pos;
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    printf("Enter the elements: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    printf("Enter the element to be inserted: ");
    scanf("%d", &x);
    printf("Enter the position at which to insert: ");
    scanf("%d", &pos);
    insert(arr, n, x, pos);
    return 0;
}

---------------×------------------××--------------------×---------------

26. SLL create,display,count,search,insert at begin , insert at end .

Ans.:- 

#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
    int data;
    struct node *next;
}node;
int *create(int n)
{
    struct node *head,*p;
    int x,i;
    head=(node *)malloc(sizeof(node));
    printf("Enter node data = ");
    scanf("%d",&x);
    head->data=x;
    head->next=NULL;
    p=head;
    for(i=2;i<=n;i++)
    {
        printf("Enter nodes data =");
        scanf("%d",&x);
        p->next=(node *)malloc(sizeof(node));
        p=p->next;
        p->data=x;
        p->next=NULL;
    }
    printf("\nsuccess\n");
    return(head);
}
int *display(node *p)
{
    if(p==NULL)
    {
        printf("\n linked list is empty\n");
    }
    else
    {
    printf("\n SLL nodes = ");
    while (p!=NULL)
    {
        printf("%d -> ",p->data);
        p=p->next;
    }
    printf("\n");
    }
}
int *count(node *p)
{
    int j=0;
    if(p==NULL)
    {
        printf("\n linked list is empty\n");
    }
    else
    {
        while (p!=NULL)
    {
        j++;
        p=p->next;
    }
    printf("\nNode Count = %d\n",j);
    }
}
int *search(node *p,int key)
{
    int j=0;
    
        while(p!=NULL)
        {
            j++;
            if(p->data==key)
            {
                printf("\n Element Found at %d \n",j);
                break;
            }
            p=p->next;
        }
        if(p==NULL)
        {
                printf("\nelement not found\n");
        }
}
int *insertatbegin(node *head)
{
    node *q;
    int x;
    q=(node *)malloc(sizeof(node));
    printf("Enter data = ");
    scanf("%d",&x);
    q->data=x;
    q->next=NULL;
    if(head==NULL)
    {
        head=q;
        return (head);
    }
    else
    {
        q->next=head;
        head=q;
        printf("\n successful \n");
        return (head);
    }
}
int *insertatend(node *head)
{
    node *q,*p;
    int x;
    q=(node *)malloc(sizeof(node));
    printf("Enter data = ");
    scanf("%d",&x);
    q->data=x;
    q->next=NULL;
    if(head==NULL)
    {
        head=q;
        return (head);
    }
    else
    {
        p=head;
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=q;
        printf("\n successful \n");
        return (head);
    }
}
int main() 
{
   int no,op;
   struct node *Head;
   Head=NULL;
   do
   {
       printf("\n1. create linked list \n ");
       printf("\n2. display linked list\n ");
       printf("\n3. To count no. of nodes \n");
       printf("\n4. search a node in linked list \n");
       printf("\n5. insert at begin in linked list \n");
       printf("\n6. insert at end in linked list \n");
       
       printf("11. exit\n");
       printf("\n\n Enter your choice = ");
       scanf("%d",&op);
       switch(op)
       {
           case 1 :
                    printf("\nEnter size of linked list = ");
                    scanf("%d",&no);
                    Head=create(no);
                    break;
                    
           case 2 : 
           display(Head);
           break;
           
           case 3 :
           count(Head);
           break;
           
           case 4 :
           printf("Enter element to search = ");
           scanf("%d",&no);
           search(Head,no);
           break;
           
           case 5: Head=insertatbegin(Head);
           break;
           
           case 6 : Head = insertatend(Head);
           break;
           
           case 11 : printf("\nExited\n");
           break;
           
           default : printf("Enter valid option");         
       }
   }while(op!=11);
      
   return 0;
}

---------------×------------------××--------------------×---------------

27. C Program of Create and display SLL.

Ans.:- 

#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
    int data;
    struct node *next;
}node;
int *create(int n)
{
    struct node *head,*p;
    int x,i;
    head=(node *)malloc(sizeof(node));
    printf("Enter node data = ");
    scanf("%d",&x);
    head->data=x;
    head->next=NULL;
    p=head;
    for(i=2;i<=n;i++)
    {
        printf("Enter nodes data =");
        scanf("%d",&x);
        p->next=(node *)malloc(sizeof(node));
        p=p->next;
        p->data=x;
        p->next=NULL;
    }
    printf("\nsuccess\n");
    return(head);
}
int *display(node *p)
{
    if(p==NULL)
    {
        printf("\n linked list is empty");
    }
    else
    {
    printf("\n SLL nodes = ");
    while (p!=NULL)
    {
        printf("%d -> ",p->data);
        p=p->next;
    }
    printf("\n");
    }
}
int main() 
{
   int no,op;
   struct node *Head;
   Head=NULL;
   do
   {
       printf("\n1. create linked list \n ");
       printf("2. display linked list\n ");
       printf("11. exit\n");
       printf("\n\n Enter your choice = ");
       scanf("%d",&op);
       switch(op)
       {
           case 1 :
                    printf("\nEnter size of linked list = ");
                    scanf("%d",&no);
                    Head=create(no);
                    break;
           case 2 : 
           display(Head);
           break;
           
           case 11 : printf("\nExited\n");
           break;
           default : printf("Enter valid option");         
       }
   }while(op!=11);
      
   return 0;
}

---------------×------------------××--------------------×---------------

28. C Program of Fibonacci Series.

Ans.:- 


#include <stdio.h>

int fibonacci(int n) 
{
   if(n == 0)
      return 0;
   else if(n == 1)
      return 1;
   else
      return (fibonacci(n-1) + fibonacci(n-2));
}

int main() 
{
   int n;

   printf("Enter the number of terms\n");
   scanf("%d", &n);

   printf("Fibonacci Series: ");
   
   for (int i = 0; i < n; i++) 
   {
      printf("%d ", fibonacci(i));
   }
   
   return 0;
}

---------------×------------------××--------------------×---------------

29. C Program of GCD.

Ans.:- 

#include <stdio.h>
 
int gcd(int, int);
 
int main()
{
    int a, b, result;
 
    printf("Enter the two numbers to find their GCD: ");
    scanf("%d%d", &a, &b);
    result = gcd(a, b);
    printf("The GCD of %d and %d is %d.\n", a, b, result);
}
 
int gcd(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            return gcd(a - b, b);
        }
        else
        {
            return gcd(a, b - a);
        }
    }
    return a;
}

---------------×------------------××--------------------×---------------

---------------×------------------××--------------------×---------------

Data Structure Using C Assignments

---------------×------------------××--------------------×---------------

Assignment No. 3 and 4
















---------------×------------------××--------------------×---------------

Data Structure using C Practical Programs

---------------×------------------××--------------------×---------------

1.Write a ‘C’ program to perform following Operations on Array: Create, Insert, Delete, Display 

Ans.



---------------×------------------××--------------------×---------------
Write a ‘C’ Program to Search a particular data from the given Array of numbers using: Linear Search Method.

Code :- 



---------------×------------------××--------------------×---------------

2. Write a ‘C’ Program to Sort an Array of numbers using Bubble Sort Method

Code :- 


---------------×------------------××--------------------×---------------

3. Write a ‘C’ Program to Sort an Array of numbers using Selection Sort Method 

Code :- 



---------------×------------------××--------------------×---------------

4.  Write a ‘C’ Program to Sort an Array of numbers using Insertion Sort Method

Code :- 



---------------×------------------××--------------------×---------------

5. Write a 'C' Program to Implement Singly Linked List with Operations: (i) Insert at beginning, (ii) Search, (iii) Display

Code:- 


---------------×------------------××--------------------×---------------

6. Write a C Program to Implement Singly Linked List with Operations: (i) Insert at end, (ii) Insert After, (iii) Delete (iv) Display

Code :-


---------------×------------------××--------------------×---------------

7. Write a 'C' Program to perform PUSH and POP Operations on Stack using an Array

Code :-


---------------×------------------××--------------------×---------------
8. Write a 'C' program to perform multiplication of two numbers using recursion

Code :-


---------------×------------------××--------------------×---------------
9. Write a 'C' Program to perform INSERT and DELETE Operations on Linear Queue using an Array

Code :-


---------------×------------------××--------------------×---------------
10. Write a 'C' Program to perform INSERT and DELETE operations on Circular Queue using an Array

Code :-


---------------×------------------××--------------------×---------------
11. Write a 'C' Program to Implement BST (Binary Search Tree) and Traverse in In-Order

Code :-


---------------×------------------××--------------------×---------------
12. Write a 'C' Program to Implement BST (Binary Search Tree) and Traverse in Pre-Order

Code :-


---------------×------------------××--------------------×---------------
13. Write a 'C' Program to Implement BST (Binary Search Tree) and Traverse in Post-Order

Code :-


---------------×------------------××--------------------×---------------
14. Write a 'C' program to perform factorial of a number using recursion

Code :-


---------------×------------------××--------------------×---------------

---------------×------------------××--------------------×---------------

---------------×------------------××--------------------×---------------

Post a Comment

0 Comments