Javatpoint Logo
Javatpoint Logo

Data Structure in C

C data structure A data structure in C is a method of arranging and conserving data in a computer so that it may be quickly accessed and altered.

Classified into two types:

  • Linear Data Structures
  • Non- Linear Data Structures

Linear Data Structures

A linear data structure in C programming is one where the data pieces are ordered sequentially or linearly. Arrays, linked lists, stacks, and queues are a few examples of linear data structures used in C.

1. Arrays:

In C, arrays are used to store a predetermined number of identically typed elements.

Characteristics:

  • Stores a fixed number of elements of the same data type
  • Allows random access to elements using index
  • Can be used to represent matrices and other mathematical structures

Advantages:

  • Simple and easy to use
  • Provides fast access to elements using index
  • Can be used to implement various algorithms, including sorting and searching algorithms

Disadvantages:

  • Fixed size, which can be inefficient if the size of the data set is unknown or dynamic
  • Insertion and deletion of elements can be expensive if the array is large

Here is an example of an array that stores 5 integers:

C Program:

Output:

2 4 6 8 10

2. Linked Lists:

Linked lists in C are used to store a collection of elements in a dynamic way. Linked lists are useful for implementing dynamic data structures that can be easily modified.

Characteristics:

  • Dynamic data structure that can grow or shrink as needed
  • Provides fast insertion and deletion of elements
  • Can be easily traversed using pointers

Advantages:

  • Provides efficient memory management
  • Allows you to create dynamic data structures that can be easily modified
  • Provides fast insertion and deletion of elements

Disadvantages:

  • Random access to elements is inefficient
  • Can be memory-intensive if used to represent large data sets

Here is an example of a linked list that stores integers.

C Program:

Output:

1 2 3

3. Queues:

Queues in C are used to store a collection of elements in a first-in, first-out (FIFO) order. For implementing algorithms that need a processing order, queues are helpful.

Characteristics:

  • Stores a collection of elements in a first-in, first-out (FIFO) order
  • Can be used to implement a wide range of algorithms and data structures
  • Provides efficient memory management

Advantages:

  • Provides a simple and efficient way to implement algorithms that require a FIFO order
  • Can be easily implemented using an array or a linked list
  • Provides fast access to the first element in the queue

Disadvantages:

  • Random access to elements is inefficient
  • Can be inefficient if used to implement algorithms that require random access to elements

Here is an example of a queue that stores integers:

C Program:

Output:

Dequeued item: 1
Dequeued item: 2
Dequeued item: 3

4. Stacks:

Last-in, first-out (LIFO) order is used to store a group of elements in stacks in the C programming language. For implementing algorithms that need a hierarchical ordering, stacks are helpful.

Characteristics:

  • Stores a collection of elements in a last-in, first-out (LIFO) order
  • Can be used to implement a wide range of algorithms and data structures
  • Provides efficient memory management

Advantages:

  • Provides a simple and efficient way to implement algorithms that require a LIFO order
  • Can be easily implemented using an array or a linked list
  • Provides fast access to the top element in the stack

Disadvantages:

  • Random access to elements is inefficient
  • Can be inefficient if used to implement algorithms that require random access to elements

Here is an example of a stack that stores integers:

C Program:

Output:

Popped item: 3
Popped item: 2
Popped item: 1

Non- Linear Data Structures

Non-linear data structures are those where the elements are not organized in a linear or sequential manner, unlike arrays and linked lists. Instead, the elements may be arranged in a hierarchical, tree-like, or graph-like structure.

Non-linear data structures include, for instance:

1. Trees:

A tree is a hierarchical data structure in which every node has at least one child and at least one parent. The bottom nodes are referred to as leaves, and the uppermost node is known as the root. Uncommon tree data structures include binary trees.

  • Characteristics: Trees are hierarchical data structures made up of nodes, each of which has a parent and zero to many offspring. In trees, leaf nodes and roots both lack parents and have no offspring. Binary trees (each node has a maximum of two children) and n-ary trees (each node has a maximum of n children) are two types of trees that can be categorised based on the maximum number of children that each node can have.
  • Advantages: Trees are useful for representing hierarchical relationships between data, such as file systems, organization charts, and family trees. Additionally, they are employed in search algorithms with logarithmic time complexity for searching, introducing, and removing elements, like binary search trees.
  • Disadvantages: Trees can have poor worst-case performance if they become unbalanced, meaning that one branch of the tree is much longer than the others. This can result in linear time complexity for search, insertion, and deletion operations.

Here is an example of tree:

C Program:

Output:

Inorder traversal 
4 ->2 ->1 ->3 ->
Preorder traversal 
1 ->2 ->4 ->3 ->
Postorder traversal 
4 ->2 ->3 ->1 ->

2. Graphs:

A graph is made up of nodes, or vertices, and the connecting edges. We know that graphs can be cyclic or acyclic, directed or undirected and also weighted or unweighted.

  • Characteristics: Graphs are nothing but the collections of the nodes (also known as the vertices) connected by the edges. Edges can include weights to represent the cost or distance between vertices and can be either directed or undirected. Graphs can be classified based on the presence of cycles, such as acyclic graphs (no cycles) and cyclic graphs (one or more cycles).
  • Advantages: Graphs are useful for representing complex relationships between data, such as social networks, transportation networks, and computer networks. They are also used in algorithms for shortest path, minimum spanning tree, and network flow problems.
  • Disadvantages: Graphs can be difficult to manipulate and analyse because of their complexity. Algorithms for graph problems can have high time and space complexity, especially for large graphs.

Here is an example of a graph:

C Program:

Output:

(0 ?> 1)	
(1 ?> 2)	
(2 ?> 1)	(2 ?> 0)	
(3 ?> 2)	
(4 ?> 5)	
(5 ?> 4)

3. Heaps:

A heap is a specialized tree-based data structure that satisfies the heap property. In a min-heap, the parent node is always smaller than its children, and in a max-heap, the parent node is always larger than its children.

  • Characteristics: Heaps are tree-based data structures that satisfy the heap property, which is that the value of each parent node is greater than or equal to the value of its children (for a max heap) or less than or equal to the value of its children (for a min heap). Heaps are often implemented as binary trees, with each node representing an element of the heap.
  • Advantages: Heaps are useful for implementing priority queues, where elements are removed from the queue in order of priority (i.e., the highest or lowest value). They are also used in sorting algorithms, such as heapsort, which has a time complexity of O(n log n).
  • Disadvantages: Heaps can have poor worst-case performance for inserting elements, especially if the heap becomes unbalanced. Also, because heaps are typically implemented as binary trees, they can have wasted space if the number of elements in the heap is not a power of two.

Here is an example of heap:

C Program:

Output:

Max-Heap array: 9 5 4 3 2 
After deleting an element: 9 5 2 3

These are just a few examples of non-linear data structures, and there are many more that are used in various applications.

Conclusion:

In conclusion, linear and non-linear data structures are both important in computer science and can be implemented using the C programming language.

Linear data structures, such as arrays, linked lists, and stacks, have a simple linear organization and are useful for storing data in a sequential manner. They have the advantage of fast access to elements in the middle of the structure, but can have slower performance for inserting or deleting elements in the middle of the structure.

Non-linear data structures, such as trees, graphs, and heaps, have a more complex organization and are useful for storing data with hierarchical or interconnected relationships. They have the advantage of efficient searching, insertion, and deletion operations, but can have worse performance for access to elements in the middle of the structure.

The choice of data structure depends on the specific requirements of the problem being solved. Linear data structures are more suitable for problems where data needs to be accessed sequentially or where the size of the data is known in advance. Non-linear data structures are more suitable for problems where the data has complex relationships or where efficient search, insertion, and deletion operations are required.

In C programming, both linear and non-linear data structures can be implemented using arrays, pointers, and structures. There are also many libraries and frameworks available that provide implementations of common data structures, such as the Standard Template Library (STL) in C++. Understanding the characteristics, advantages, and disadvantages of different data structures is an important skill for computer scientists and software developers.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA