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

Data structures

The document provides an overview of data structures, including linear and non-linear types such as arrays, linked lists, stacks, queues, trees, and graphs. It includes programming tasks and questions related to implementing these structures in C, along with concepts like memory allocation, pointers, and traversal methods. Additionally, it covers topics such as binary search trees, hash tables, and sorting algorithms, with various assignments and questions for further understanding.

Uploaded by

Cranky Magma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data structures

The document provides an overview of data structures, including linear and non-linear types such as arrays, linked lists, stacks, queues, trees, and graphs. It includes programming tasks and questions related to implementing these structures in C, along with concepts like memory allocation, pointers, and traversal methods. Additionally, it covers topics such as binary search trees, hash tables, and sorting algorithms, with various assignments and questions for further understanding.

Uploaded by

Cranky Magma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data structures

2025-01-06

What is a data structure?


It is the different ways of arranging data.

Types:
1. Linear:
Arrays, structure, union, Linked list, stack, queue
2. Non-Linear
Graph, tree, hash table

Question

implement a c program to store 5 integers read from the user and display the
same.

Keep in mind while programming C: C is top down, C++ is bottom up.

Todo

Assignments:

1. What is an array in c and how is memory allocated in arrays.


2. How can you determine the size of an array in C. Explain with a program.
3. How are arrays passed to functions in c
4. What is a pointer, how is it declared in C.
5. What is pointer arithmetic, provide examples.
6. What is a null pointer, why is it used?
7. What is a structure in C, how is it different from an array?
8. what is the use of typedef with structures?
9. Can a structure containa pointer to itself? example.
10. What are malloc, calloc, realloc and free?
11. What happens if you forget to free dynamically allocated memory?

2025-01-07

Linked Lists
What is a linked list
It is a linear data structure

Types

1. single linked list

> Every data is referred to as **Nodes**


> - Nodes have 2 parts, data and link to refer to the next node.

data link to next node

node

4555

10 6000

6000

20 6420

6420

30 NULL
The first node is identified by a Head pointer.
the end of the pointer is identified by its link. It will be NULL.

in linked we make nodes using struct :

struct SLL{
int data;
struct SLL *link;
}

functions that need to be implemented:

1. Insert front -> adding a new node at the beginning


2. insert end -> adding a new node to the end of the linked list
3. delete front -> remove a node at the beginning
4. delete end -> remove node at the end of the linked list.

Note

malloc returns a void pointer so typecasting is needed

2025-01-08

display function implementation.

void display(){
while(temp!=NULL){
printf("%d\n",temp->data);
temp=temp->link;
}

insert at end implementation.

void insertEnd(int d){


node* new = memalloc(d);
if(head==NULL){
head = new;
return;
}
node* ptr = head;
while(ptr->link != NULL){
ptr = ptr->link;
}
ptr->link = new;
}

2025-01-09

Question

q. create a singly linked list with as many nodes as requested by the user and
search for the data if present in the linked list or not.

2. double linked list


2025-01-13

3 parts in a node
Data
*prev
*next
traversal is possible in forward and reverse direction
*head is used to indicate first node
*prev is NULL for first node
*next is NULL for last node

2025-01-15

Question

1. Write a c program to create a doubly linked list with as many nodes as the
user want. Insert the data read by the user. Implement a menu driven
program with operations with insert front, insert end, display, reverse.
2. Implement a c program to sort the linked list in ascending and descenting
order. Display before and after the sorting.
3. circular linked list
2025-01-20

The last node will point to the first node.


For doubly Linked list,

Question

1. Create a circular linked list using a singly-linked list.


2. Create a cicular linked linked list using a doubly-linked list. Operations:
insert Front/End, delete Front/End.

2025-01-21

Stack
What is a stack?
Linear data structure
Organizes data in a Last In First Out manner. (LIFO)
The data is inserted and removed through a top pointer
basic operations:
- push: Adding data to the stack
- pop: Remove data from the stack
Implemented using either arrays or linked lists

Queue
What is a Queue?
Linear data structure
organizes data in a FIFO manner or LILO manner.
Data is inserted through rear.
Data is removed through front.
Basic operations:
- enqueue: add data to queue
- dequeue: remove data from queue
implemented using either arrays or linked lists.

Question

q. Write a C function to reverse a string using a stack.

2025-01-22

Question

q. evaluate a C program to evaluate a post-fix expression.


q. Write a C program to check if the parantheses are balanced.

2025-01-23

Question

1. Write a C program to create a Queue using two stacks. (Create a queue


using stack)(freuently asked)

Non-Linear Data Structures


Tree
Tree is a non-linear data structure.
Every element is represented as a node.
A node without a Child node is called a Leaf node.
The First node is called Root Node.
Types:
Binary Tree:*
Every node can have a maximum of 2 child/sub nodes.
Binary Search Tree: BST is a type of binary tree.

Binary Search Tree (BST)


It is a type of Binary tree.

Every node will be compared to the root node.


if greater, add it to the right sub tree
if lesser, add it to the left sub node.
No duplicates are allowed.

1. Inorder Traversal:
Left ==> Root ==> Right
Always gives data in ascending order.
2. preorder Traversal:
Root ==> Left ==> Right
3. PostOrder Traversal:
Left ==> Right ==> Root

2025-01-28

Question

1. Write a program to check if a binary tree is a binary search tree


2. Write a C program to find the Kth smallest element in a Binary Search tree.

2025-01-29

Todo

1. Write a program to delete an element from a BST

Graph
Graph is a collection of vertices and edges.
Edge: connection between two vertices.
Vertex: it holds data.
Represented using adjacency matrix.
Directional graph is when an edge has a specific direction.
1 in the graph means one is connected to the other vertex but not the other
way around.
v0 v1 V2 V3
V0 0 1 1 1
V1 0 0 1 0
V2 0 0 0 1
V3 0 0 0 0

Undirected graph is when edges have no vertices.


1 in matrix means the two arrays are connected to each other

2025-01-30

Traversal

Depth first search (DFS):


Classifies the vertices as
Visited
non- visited
Uses Stack

2025-01-31

Assignments:
2025-01-07

Question

1. Explain the structure of a node in a singly linked list. How do you initialie
it?
2. What is the difference between pass by value and pass by address in C.
Demonstrate with examples.
3. How are functions stored in memory? How is their execution handled
during run-time?
4. Differentiate between for, while and do-while loops. provide use cases for
each.
5. Explain the concept of nested loops. Write a program to print a
multiplication table using nested loops.
6. Explain the storage classes in C. Provide examples for each.
7. When should you use register storage class? Can you get the address of a
register variable? why or why not?
8. Write a program to demonstrate the use of static variables in a function.

2025-01-08

Todo

1. Explain the role of dynamic memory allocation in linked lists.


2. Why can't we use static arrays for linked lisked lists?
3. How does malloc work in C and Why is it commonly used in liked lists?
4. Explain ho pointers are used to link nodes in a singly -linked list? What
happens if we don't update the next pointer correctly during insertion?
5. What happens if you try to insert a node toan empty linked list? How do
you handle such cases?
6. How can you modify the insertFront and insertEnd functions to avoid
duplicate data entries?

2025-01-21

Question

1. Write a C program to create a stack using linked lists and perform the push
and pop operations.
2. Write a C program to create a queue using linked lists and perform the
operations enqueue dequeue and display.
3. What is a stack and how does it differ from a queue?
4. Explain the LIFO principle of a stack with an example.
5. Write a program to implement a stack using dynamic array.
6. Explain the operations of a stack with examples.
7. What happens when a stack is full , how can it be handled?
8. Differentiate between static and dynamic stack implementations.
9. Explain thee FIFO principle of a Queue with an example.
10. What happens when a queue is full, how can it be handled?
11. Explain circular queue.

2025-01-23

Question

1. Simulate a customer service system where customers arrive at random


intervals and are served in the order of their arrival. Consider the features:
a. Add a new customer.
b. Serving the customer
c. Display all waiting customers
2. Implement a c program using a queue to check if a given string is
palindrome.
3. Simulate a supermarket billing system where customers queue up to py
bills. Handle features like:
a. Adding a new customer
b. Billing a customer and removing from the queue
c. displaying the current queue

2025-01-28

Question

1. Explain the difference between a binary tree and a binary seach tree
2. what are the advantages of using binary seach tree over an array or a
linked list.
3. What is the height of a binary tree and how is it calculated.
4. Explain the difference between preorder, inorder and post order traversal.
5. What traversal of a binary search tree produces a sorted list of elements.
6. How do you insert a node into a binary search tree. Describe the algorithm.
7. How do you delete a node from a binary search tree: with no child node,
one child and with two children. Explain the algorithm.

2025-01-29
Todo

1. Write a C program to create an adjacency matrix for the given directed


graph
(table above in directed section)

2025-01-30

Question

1. What is a graph and how is it different from a tree.


2. How do you represent a tree in C
3. What are directed and undirected graph.
4. Explain the algorithm depth first search with an example.
5. What are edges in a graph?
6. Write a C program to find the number of connected components in an
undirected graph. input is no.of vertices and eges. Output: number of
connected compnents.

2025-02-05

Question

1. what is a hash table, how does it work.


2. explain the collision resolution techniques chaining and linear probing
3. what is the difference between chaining and linear probing
4. what is a hash function
5. what are the advantages and disadvantages of using a hash table
6. how does a hash table cmpare to a binary search tree in terms of
performance.
7. Write a C program to implement a hash table that resolves collision using
chaining or linear probing. Implement as a menu driven code such that the
user can make a choice of selecting chaining or linear probing if collision is
detected.
8. Write a function to search for a given key in a hash table and return its
value.
9. create a phonebook using a hash table.

2025-02-07

Todo

1. Explain the working of selection sort.


2. Can selection sort be implemented recursively. If yes Write C code for it
3. compare the selection sort with insertion sort in terms of efficiency and
usage.
4. how does selction sort behave on an array with duplicate elements?
5. Explain the working of insertion sort.
6. How does insertion sort perform when sorting a linked list?
7. Explain the difference between insertion sort and selection sortin terms of
swapping and shifting operations.
8. Write a C program to sort an array of integers using bubble sort, selsection
sort and insertion sort using a menu driven application. Also sort the data
in ascending or descending order based user choice.
9. Write a C program to sort an array of strings alphabetically using insertion
sort.
10. Given a structure student with fields name, roll no and marks. Sort an array
of students in ascending order of marks using seletion sort.
11. Sort only even indexed elements in ascending order while keeping the odd
indexed elements unchanged.
12. Find the kth smallest element in an unsorted array using selection sort.

Questions
1. What are the differences between printf and puts
2. Explain the difference between static and extern storage classes in C
3. What is the output of the following code snippet?

int a = 10;
printf("%d %d %d", a++, ++a, a);
4. How does pointer arithmetic work in C. Provide an example.
5. Malloc and calloc.
6. Explain function pointers in C with an example.
7. How does realloc function work in DMA.
8. Why is free() function necessary in C and what happens if it is not used?
9. What is the difference between pass by value and pass by address in C?
10. Can you return a local variable from a function. Why or why not?
11. How can you implement a 2D array dynamically using pointers?
12. Explain the difference between array name and pointer in C.
13. Can a function modify an array passed to it? Demonstrate with code.
14. What is a null pointer and hoe is it different from an uninitialized pointer?
15. What are the advantages of using structures over arrays?
16. How do you compare two structures in C?
17. What is the key difference between struct and union?
18. How is memory allocated for a structure with bit fields?
19. Can you create a self referential structure? Provide an example.
20. Write a function to detect a loop in a linked list.
21. How do you reverse a singly linked list?
22. Compare singly and doubly linked lists in terms of operations
23. Explain how to delete a node in a linked list given only a pointer to that node.
24. What are the advantages of circular linked lists over singly linked lists
25. How do you implement a stack using an array.
26. Implement a Queue using two stacks
27. What is the advantage of a circular queue over a linear queue
28. How can you detect stack overflow in a recursive function
29. How does merge sort use the divide and conquer approach?
30. Compare linear search and binary search in terms of efficiency
31. Implement a sorting algorithm and explain its working
32. What is the difference between a binary tree and a binary search tree?
33. Write a function to find the height of a binary tree
34. How does breadth first search differ from depth first search.
35. How would you detect a cycle in an undirected graph?
36. You have an array where each element represents the maximum steps that can be
jumped forward. Find the minimum jumps required to reach the last index.
37. Find the missing number in an array containing numbers from one to n with one
missing number.
38. You are given an array where every element appears twice except one. Find the
unique element.
39. Given a string check if it is palindrome or not.
40. A person has two hour glasses. one runs for 7 minutes and the other for 11
minutes. How can they time exactly 15 minutes.
41. You have 3 bulbs in a room and 3 switches outside. You can only enter the room
once. How do you determine which switch controls which bulb
42. A train leaves station A at 6am and reach station B at 10am. another train leave B
at 11am and reaches A at 12pm at what time will the two trains meet.
43. There are 100 lockers and 100 students. The first student opens all the lockers.
Toggles all the 2nd locker. The 3rd student toggles every 3rd locker and so on.
Which lockers remain opened at the end.
44. You have a 3 litre jug and a 5 litre jug. How do you measure exactly 4 litres using
these jugs?

You might also like