Professional Documents
Culture Documents
CS12101
Module 01
Data Structure
Topics
● Data
● Data Type
● Introduction to Data Structure
● Data Structure – Need and Application
● Compile Time and Run Time
● typedef
● Memory Layout of C Program
● Pointer
● Dynamic Memory Allocation
● Concept of Linked List, Queue, and Stack
Data
for example, student's name and its id are the data about the student
Primitive DS Non-Primitive DS
● Arrays ● Trees
● Linked Lists ● Graphs
● Stacks
● Queues
Types of Data Structure
The data items are arranged in sequential The data items are arranged in non-sequential
order, one after the other. order (hierarchical manner).
All the items are present on the single layer. The data items are present at different layers.
It can be traversed on a single run. That is, if It requires multiple runs. That is, if we start
we start from the first element, we can from the first element it might not be possible
traverse all the elements sequentially in a to traverse all the elements in a single pass.
single pass.
The memory utilization is not efficient. Different structures utilize memory in different
efficient ways depending on the need.
The time complexity increase with the data Time complexity remains the same.
size.
● Processor speed:
○ To handle very large amount of data, high speed processing is required, but as the data is
growing day by day to the billions of files per entity, processor may fail to deal with that much
amount of data
● Data Search:
○ Consider an inventory size of 106 items in a store, If our application needs to search for a
particular item, it needs to traverse 106 items every time, results in slowing down the search
process.
● Multiple requests:
○ If thousands of users are searching the data simultaneously on a web server, then there are
the chances that a very large server can be failed during that process
all items are not required to be searched and required data can be
searched instantly
● Compile-time errors are the errors that occurred when we write the wrong
syntax
○ If we write the wrong syntax or semantics of any programming language, then the compile-
time errors will be thrown by the compiler
○ The compiler will not allow to run the program until all the errors are removed from the
program
○ When all the errors are removed from the program, then the compiler will generate the
executable file
● Compile-time errors
○ Syntax error
■ When the programmer does not follow the syntax of any programming language, then
the compiler will throw the syntax error
■ int a, b:
○ Semantic errors
■ The semantic errors exist when the statements are not meaningful to the compiler
■ a+b=c;
● C programming language as it can contain only one variable on the left of the
assignment operator while right of the assignment operator can contain more than
one variable
● Run-time errors
○ The runtime errors are the errors that occur during the execution and after compilation
■ Division by zero,
■ Determining the square root of a negative number, etc.
○ These errors are not easy to detect as the compiler does not point to these errors
unit a, b;
instead of writing:
unsigned int a, b;
Using, typedef
● When we run any C-program, its executable image is loaded into RAM of
computer in an organized manner
● Static memory persists throughout the entire life of the program, and is usually
used to store things like global variables, or variables created with the static
clause. int theforce;
○ This variable uses 4 bytes of memory.
○ This memory can come from one of two places.
■ If a variable is declared outside of a function, it is considered global, meaning it is accessible
anywhere in the program. Global variables are static, and there is only one copy for the entire
program.
■ Inside a function the variable is allocated on the stack. It is also possible to force a variable to
Pointers
● Pointers (pointer variables) are special variables that are used to store
addresses rather than values.
● Lets declared a pointer p of int type
Warning
c = 22;
c = 11; *pc = 2;
#include <stdio.h>
int main() {
int c = 5;
int *p = &c;
printf("%d", *p);
return 0;
}
● Pointer variable size is not depending on data type as pointer always stores the address of other variable
● So, any pointer (int, char, double, etc) size will be 2 for 16 bit processor, 4 for 32 bit processor and 8 for 64
bit processor.
● A char pointer variable holds only the address of char type variable.
● But still the address of all these type of pointer variable is number.
numbers.
Syntax:
● The malloc function allocates space for an object whose size is specified by
size.
● The malloc() function reserves a block of memory of the specified
number of bytes. And, it returns a pointer of void which can be casted
into pointers of any form.
● If there is no space available, the malloc function return NULL.
Syntax:
● The realloc function is different from the malloc and calloc, it deallocates the
old object and allocates again with the newly specified size.
● If the dynamically allocated memory is insufficient or more than required, you
can change the size of previously allocated memory using the realloc()
function.
● If piData is the null pointer, the realloc behaves like the malloc function.
● If piData not pointing a dynamically allocated memory, the behavior of realloc
is undefined.
● If piData is pointing a deallocated memory ( memory block has been
deallocated), the behavior of realloc is undefined.
● The return value of the realloc function is the pointer to a new object, or a null
pointer if the new object could not be allocated.
Syntax:
free(ptr);
● structures that have one or more pointers which point to the same type of
structure, as their member
● structures pointing to the same type of structures are self-referential in nature
● Self-referential structure plays a vital role in the linked list, trees, graphs, and
many more data structures
● In this type of structure, the object of the same structure points to the same data
structure and refers to the data types of the same structure.
● It can have one or more pointers pointing to the same type of structure as their
member.
Dr. Anand Kumar Mishra
In a linked list, the structure is used to store the data information and the address of the next node in the given
linked list.
Mainly the data part is of integer type, and the link address part is of pointer type, which holds the address of the
next node, so that it can for the Link ultimately.
struct node * link ; // Self referential structure link is pointing the same structure of the type node
};
int main()
struct node obj; // Creating an object of the node type, three parts one is data 1, data 2, and 3rd is of link type pointer
Return 0 ;
● A data item
● An address of another node
struct node {
int data;
struct node *next;
} Dr. Anand Kumar Mishra
Doubly Linked List
struct node {
int data;
struct node *next;
struct node *prev;
} Dr. Anand Kumar Mishra
Circular Linked List
● A circular linked list is a variation of a linked list in which the last element is
linked to the first element
● This forms a circular loop
● It is used in multiplayer games to give a chance to each player to play the game.
● Multiple running applications can be placed in a circular linked list on an operating
system. The os keeps on iterating over these applications.
Dr. Anand Kumar Mishra
Representation of Linked List
● A data item
● An address of another node
struct node
{
int data;
struct node *next;
};
create a simple Linked List with three items
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
/* Assign data values */
one->data = 1;
two->data = 2;
three->data=3;
/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;
/* Save address of first node in head */
head = one; Dr. Anand Kumar Mishra
Dr. Anand Kumar Mishra
Dr. Anand Kumar Mishra
Dr. Anand Kumar Mishra
Queue
Dr. Anand Kumar Mishra
Queue
A queue is an object (an abstract data structure - ADT) that allows the following
operations:
Working of Queue
● two pointers FRONT and REAR
● FRONT track the first element of the queue
● REAR track the last element of the queue
● initially, set value of FRONT and REAR to -1
1. A pointer called TOP is used to keep track of the top element in the stack.
2. When initializing the stack, we set its value to -1 so that we can check if the stack
is empty by comparing TOP == -1.
3. On pushing an element, we increase the value of TOP and place the new element
in the position pointed to by TOP.
4. On popping an element, we return the element pointed to by TOP and reduce its
value.
5. Before pushing, we check if the stack is already full
6. Before popping, we check if the stack is already empty
● To reverse a word
○ Put all the letters in a stack and pop them out. Because of the LIFO order of stack, you will get the
letters in reverse order.
● In compilers
○ Compilers use the stack to calculate the value of expressions like 2 + 4 / 5 * (7 - 9) by converting the
expression to prefix or postfix form.
● In browsers
○ The back button in a browser saves all the URLs you have visited previously in a stack. Each time
you visit a new page, it is added on top of the stack. When you press the back button, the current
URL is removed from the stack, and the previous URL is accessed.