Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What are Data Structures in C & How to Use Them?

Updated on 03 July, 2023

5.86K+ views
10 min read

Introduction

To start with, a data structure is a collection of data items that are kept together under one name or heading and is a specific way of storing and assembling the data so that the data can be used efficiently.

What is C Programming Language

C is a procedural programming language which was developed in 1972 by Dennis Ritchie. It is among the most popularly used programming languages today and is an excellent choice for building data-centric applications. C provides low-level access to memory, making it well-suited for applications that require precise control over how data is manipulated and stored in memory.

One of the major advantages of using C is its support for data structures. Data structures are collections of related information that can be accessed and modified quickly and easily. C supports several types of data structures, including arrays, structs, linked lists, trees, stacks, queues and graphs. Struct in C programming is particularly useful when it comes to assembling large amounts of data into meaningful chunks.

Check out our free courses to get an edge over the competition 

Types

Data Structures are prevalent and used in almost every software system. Some of the most common examples of Data Structures are arrays, queues, stacks, linked lists, and trees.

Applications

In designing compilers, operating systems, making databases, Artificial Intelligence applications, and many more.

Benefits of Data Structures in C:

Data structures in C provide multiple benefits and are essential to developing efficient, robust programs. Some of the main advantages include:

  • Improved Efficiency 

Data structures in C such as linked lists and hash tables can significantly improve data processing speed by reducing search time complexity and allowing more direct access to information. This is especially important for large datasets that may take considerable time to process without these optimized data structures.

  • Easier Debugging 

Structured data helps developers spot errors quickly, eliminating long debugging sessions. It even makes it easier to identify and address problems before they become major issues that require in-depth code reworking. 

  • Reusability 

Data Structures using C enable developers to create reusable code that can be applied and adapted to different situations. This eliminates the need to rewrite code for each new project, saving time and resources.

  • Greater Flexibility 

With structured data, developers can quickly adjust programs as needs change and create new solutions based on existing data structures. This is an incredible way to extend the functionality of an application without creating more overhead in terms of complexity or development time.

Using C for programming offers powerful tools that make use of these data structures and can provide significant performance improvements compared to other languages. Understanding how they work and the associated benefits and potential pitfalls is essential for developing efficient applications that can meet modern software development’s rigours. Moreover data structures using C can help developers create more robust code that is easier to debug, reuse, and modify.

Check out upGrad’s Java Bootcamp 

Classification

Data Structures are classified into two categories: primitive data structures and non-primitive data structures.

1. Primitive: They are the basic data types that are supported by a programming language. A common example of this classification is integers, characters, and boolean.

2. Non-Primitive: These categories of data structures are created using primitive data structures. Examples include linked stacks, linked lists, graphs, and trees.

Arrays

An array is a simple collection of data elements that have the same data type. That means an array of type integers can only store integer values. An array of data type float can store values that correspond to float data type and nothing else.

Elements stored in an array are linearly accessible and are present in contiguous blocks of memory that can be referred to using an index.

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN) 

Declaring an Array

In C, an array can be declared as:

data_type name[length];

For example,

int orders[10];

The above line of code creates an array of 10 memory blocks in which an integer value can be stored. In C, the array index value starts from 0. So the index values will range from 0 to 9. If we want to access any particular value in that array, we simply have to type:

printf(order[index_number]);

Another way to declare an array is as follows:

data_type array_name[size]={list of values};

For example,

int marks[5]={9, 8, 7, 9, 8};

The above line of command creates an array having 5 memory blocks with fixed values in each of the blocks. On a 32 bit compiler, the 32-bit memory occupied by int data type is 4 bytes. So, 5 blocks of memory would take up 20 bytes of memory. 

Another legit way of initializing arrays is:

int marks [5] = {9 , 45};

This command will create an array of 5 blocks, with the last 3 blocks having 0 as their value. 

Another legit way is:

int marks [] = {9 , 5, 2, 1, 3,4};

The C compiler understands that only 5 blocks are required to fit these data into an array. It will therefore initialize an array of name marks of size 5. 

Similarly, a 2-D array can be initialized in the following way

int marks[2][3]={{9,7,7},{6, 2, 1}};

The above command will create a 2-D array having 2 rows and 3 columns.

Read: Data Structure Project Ideas & Topics

Operations

There are some operations that can be performed on arrays. For example:

  1. traversing an array
  2.  Inserting an element in the array
  3.  Searching for a particular element in the array
  4. Deleting a particular element from the array
  5. Merging the two arrays and,
  6. Sorting the array — in ascending or descending order.

Disadvantages

Memory allocated to the array is fixed. This actually is a problem. Say, we created an array of size 50 and only accessed 30 blocks of memory. The remaining 20 blocks take up memory without any use. Therefore, to tackle this problem, we have a linked list. 

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

Linked List

Linked List, very much like arrays stores data serially. The main difference is that it does not store everything all at once. Instead stores the data or makes a memory block available as and when required. In a linked list, the blocks are divided into two parts. The first part contains the actual data.

The second part is a pointer that points to the next block in a linked list. The pointer stores the address of the next block that holds the data. There is one more pointer known as the head pointer. head points to the first block of memory in the linked list. Following is the representation of the linked list. These blocks are also referred to as ‘nodes’.

Initializing Linked Lists

To initialize the link list, we create a structure names node. The structure has two things. 1. The data that it holds and 2. The pointer that points to the next node. The data type of pointer will be that of the structure as it is pointing to the structure node.

struct node 
{ 
int data;
 struct node *next; 
};

In a linked list, the last node’s pointer will not point to anything, or simply, will point to null. 

Also Read: Graphs in Data Structure

Linked List Traversal

In a linked list, the last node’s pointer will not point to anything, or simply, it will point to null. So to traverse an entire linked list, we create a dummy pointer that initially points to the head. And, for the length of the linked list, the pointer continues to move forward until it points to null or reaches the last node of the linked list.

Adding a Node

The algorithm to add a node before a specific node would be as follows:

  1. set two dummy pointers(ptr and preptr) that point to head initially
  2. move the ptr till ptr.data is equal to the data before we intend to insert the node. preptr will be 1 node behind ptr. 
  3. Create a node 
  4. The node to which the dummy preptr was pointing, that node’s next will point to this new node 
  5. New node’s next will point to the ptr. 

The algorithm for adding a node after a particular data would be done in a similar way. 

Advantages of Linked List 

  1. Dynamic size unlike an array
  2. Performing insertion and deletion are easier in the linked list than in an array. 

Queue 

Queue follows a First In First Out or FIFO type of system. In an array implementation, we will have two pointers to demonstrate the use case of Queue. 

Source

FIFO basically means that the value that enters the stack first, leaves the array first. In the above queue diagram, the pointer front points to the value 7. If we delete the first block (dequeue), the front will now point to the value 2. Similarly, if we enter a number (enqueue), say, 3 in position 5. Then, the rear pointer will point at position 5. 

Overflow and Underflow Conditions

Nevertheless, prior to entering a data value in the queue, we must check for overflow conditions. An overflow will occur when there is an attempt to insert an element into a queue that is already full. A queue will full when rear = max_size–1.

Likewise, before deleting data from the queue, we should check for underflow conditions. An underflow will occur when there is an attempt to delete an element from a queue that is already empty, i.e. if front = null and rear = null, then the queue is empty.

Stack

A stack is a data structure in which we insert and delete elements only at one end, also known as the top of the stack. Stack implementation is therefore referred to as a last-in, first-out (LIFO) implementation. Unlike queue, for the stack, we only require one top pointer.

If we want to enter (push) elements in an array, the top pointer moves up or increments by 1. If we want to delete(pop) an element, the top pointer decrements by 1 or goes down by 1 unit. A stack supports three basic operations: push, pop, and peep. Peep operation is simply displaying the topmost element in the stack. 

Source

Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

In this article, we have talked about 4 types of data structures namely, arrays, linked lists, queues, and stacks. Hope you liked this article and stay tuned for more interesting reads. Until next time.

If you’re interested to learn more about Javascript, full-stack development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Frequently Asked Questions (FAQs)

1. What are data structures in programming?

Data structures are the way we arrange data in a program. The two most important data structures are arrays and linked lists. Arrays are the most familiar data structure, and it is the easiest to understand. Arrays are basically numbered lists of related items. They are simple to understand and use, but they are not very efficient when working with large amounts of data. Linked lists are more complex, but they can be very efficient if used properly. They are good choices when you will have to add or remove items in the middle of a large list, or when you need to search for items in a large list.

2. What are the differences between linked list and arrays?

In arrays, an index is used to access an element. Elements in the array are organized in sequential order, which makes it easy to access and modify elements if an index is used. Array also has a fixed size. Elements are allocated at the time of its creation. In the linked list, a pointer is used to access an element. Elements of a linked list are not necessarily stored in sequential order. A linked list has an unknown size because it can contain nodes at the time of its creation. A pointer is used to access an element, so memory allocation is easier.

3. What is a pointer in C?

A pointer is a data type in C which stores the address of any variable or function. It is generally used as a reference to another memory location. A pointer can hold a memory address of an array, structure, function or any other type. C uses pointers to pass values to and receive values from functions. Pointers are used to dynamically allocate memory space.

RELATED PROGRAMS