Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Data Structures

Rating
-
Sold
-
Pages
5
Uploaded on
17-05-2023
Written in
2022/2023

Provide an indepth knowledge of Data structures in a very short form .

Institution
Course

Content preview

Data Structure in C | Data Structures and Algorithms | C Programming

Data Structures using C Programming Language

C programming language is a flexible and well-established language, making it the
mother of all programming languages. Many essential libraries, firm wills, and
operating systems are implemented using C. For example, in Linux, more than 85
percent of the job is done using C. To get into tech giants like Google, Amazon,
Facebook, or Microsoft, you need to have a solid knowledge of data structures, and
that's what we will cover in this session.

C Programming Language
C programming language was designed by Dennis Ritchie in 1972. It is a procedural
language that requires following a set of steps to get the desired output. C
programming language is much faster than other languages, making it ideal for
handling low-level activities and allocating memory. Operating systems like Unix,
RDBMS, and Pi Square are written in C.

Data Structures
Data structures are a way to arrange data in computers, involving arranging data in
a particular structure. There are two types of data structures: linear and
nonlinear data structures. Linear data structures have a sequence or order to them,
while nonlinear data structures have a hierarchical ordering. Some common data
structures include arrays, linked lists, stacks, queues, binary trees, binary
search trees, hashing, and graphs.

Arrays
Arrays are stored in contiguous memory allocations, meaning the data has to be
stored in sequence and in order in memory. Elements in the array can be accessed
randomly using their index. Arrays can only store homogeneous data types, meaning
only one data type can be stored in a single array. When declaring an array, the
syntax includes the data type, name, and size. Arrays are static and cannot be
dynamic.

In programming, arrays can be declared and initialized in a single line, without
specifying the size. This allows for random access and makes sorting and iterating
easy. Arrays are suitable for storing information in a linear fashion and for
applications that require frequent searching using algorithms such as binary search
and linear search.

However, arrays have their disadvantages, such as the need to fix the size and the
difficulty of deleting or inserting elements. If an array is declared with a size
of 50 and you try to store 51 elements, it won't work. Additionally, arrays require
contiguous memory locations to store elements, and if there are chunks of memory
available but not contiguous, they can't be used to store array elements. If an
array is declared with a size of 50 but only 20 elements are stored, some memory is
wasted.

To declare an array in C++, use "int" followed by the array name and size. When
inserting elements into the array, use an ampersand sign to get the address and
store the element at that position. When accessing elements, use the base address
and specify the index.

A bonus question is provided, which asks how to find an element in an array where
every element is repeated twice except for one. The optimal solution is to use the
XOR operator, where elements that are the same when XORed with each other will give
0, and elements that are different will produce a non-zero result.

Linear Data Structures: Arrays and Linked Lists

, Arrays
An array is a linear data structure that can store homogeneous elements in a
contiguous memory block. It has a fixed size and requires that elements be stored
in a specific order. However, there are limitations to arrays such as the need to
allocate memory beforehand and the inability to efficiently insert or delete
elements within the array.

Linked Lists
Linked lists are linear data structures that can store elements in a non-contiguous
memory block. Each element, called a node, contains a data field and a pointer to
the next node. Linked lists are dynamic in size, allowing for efficient insertion
and deletion of elements. However, they require traversal to access elements and
have a key head node that should not be changed.

Advantages and Disadvantages
Arrays
Efficient random access to elements
Fixed size and requires contiguous memory allocation
Inefficient insertion and deletion of elements
Linked Lists
Dynamic in size and efficient insertion and deletion of elements
Non-contiguous memory allocation and requires traversal to access elements
Has a key head node that should not be changed
Demonstration
To demonstrate linked lists, we use a skeleton node structure with a data field and
a pointer to the next node. We also include the standard library header to use the
malloc function for memory allocation. We then define a display function to
traverse through the linked list and print out each element.


#include<stdio.h>#include<stdlib.h>typedef struct node { int data; struct node*
next;} Node;void display(Node* head) { Node* current = head; while(current !=
NULL) { printf("%d ", current->data); current = current->next; }}int main()
{ Node* head = NULL; Node* second = NULL; Node* third = NULL; head =
(Node*)malloc(sizeof(Node)); second = (Node*)malloc(sizeof(Node)); third =
(Node*)malloc(sizeof(Node)); head->data = 1; head->next = second; second-
>data = 2; second->next = third; third->data = 3; third->next = NULL;
display(head); return 0;}

Traversing a Linked List
To traverse a linked list, we start with the head node and check if it is null. If
it is not null, we print the data in the linked list and shift the link to the next
node. We repeat this process until we reach the end of the list.

Here is an example:

struct Node { int data; struct Node* next; }; struct Node*
head = NULL; struct Node* second = NULL; struct Node* third = NULL; //
allocate memory for nodes head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node)); third = (struct
Node*)malloc(sizeof(struct Node)); // assign values to nodes head->data =
10; head->next = second; second->data = 20; second->next = third;
third->data = 30; third->next = NULL; // display the linked list void
display(struct Node* head) { struct Node* temp = head; while(temp !=
NULL) { printf("%d ", temp->data); temp = temp->next; } }
display(head); // Output: 10 20 30
Finding the Middle Element in a Linked List
To find the middle element in a linked list, we use two pointers: a slow pointer
that moves one node at a time and a fast pointer that moves two nodes at a time.

Written for

Institution
Course

Document information

Uploaded on
May 17, 2023
Number of pages
5
Written in
2022/2023
Type
SUMMARY

Subjects

$9.39
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
siddharthbollam4

Get to know the seller

Seller avatar
siddharthbollam4 Mahindra University
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions