DS Notes - M-1 & 2-3-30
DS Notes - M-1 & 2-3-30
In the modern world, data and its information is an essential part, where data is just collection
of facts or set of values that are in particular format and the information is the processed data.
If the data is not organized effectively, it is very difficult to perform any task on large amount
of data. If it is organized effectively then any operation can be performed easily on that data.
If the data is stored in a well-organized way on storage media and in computer's memory then
it can be accessed quickly for processing that further reduces the latency and the user is
provided a fast response.
Data structures is about rendering data elements in terms of some relationship, for better
performance, organization and storage. The logical or mathematical model for a particular
organization of data is termed as a data structure. It represents the knowledge of data to be
organized in memory. It should be designed and implemented in such a way that it reduces
the complexity and increases the efficiency.
Data Structure is useful in representing the real-world data and its operations in the computer
program. A data structure is a storage that is used to store and organize data. It is a way of
arranging data on a computer so that it can be accessed and updated efficiently.
2
DATA STRUCTURES
An abstract data type, sometimes abbreviated ADT, is a logical description of how we view the
data and the operations that are allowed without regard to how they will be implemented. This
means that we are concerned only with what data is representing and not with how it will
eventually be constructed. By providing this level of abstraction, we are creating an
3
DATA STRUCTURES
encapsulation around the data. The idea is that by encapsulating the details of the
implementation, we are hiding them from the user’s view. This is called information hiding. The
implementation of an abstract data type, often referred to as a data structure, will require that
we provide a physical view of the data using some collection of programming constructs and
primitive data types.
A list contains elements of same type arranged in sequential order and following
operations can be performed on the list.
Implementation of List:
• Using Arrays
• Insertion and Deletion Operation are expensive as it requires more data movement.
• Even if the array is dynamically allocated, an estimate of the maximum size of the list
is required which considerably wastes the memory space.
Operations of array:
• Creation of List
4
DATA STRUCTURES
//Creation of List
void create() {
int n, list[20];
scanf("%d",&n);
for(int i=0;i<n;i++) {
scanf("%d",&list[i]);
} }
//Insertion operation
void insert() {
int i,data,pos;
create();
scanf("%d",&data);
scanf("%d",&pos);
if(pos==n) {
printf("Array overflow"); }
else {
for(int i=n-1;i>=pos;i--) {
list[i+1] = list[i]; }
list[pos]=data;
n++;
for(int i=0;i<n;i++)
printf(" %d",list[i]);
} }
5
DATA STRUCTURES
void search() {
scanf("%d",&d);
for(int i=0;i<n;i++){
if(d==list[i]) {
p=i;
found=1;
break;
} } }
void delete() {
if(found){
list[i] = list[i+1];
n--;
for(int i=0;i<n;i++)
else {
}}
• Data: data is the value stored in it, it could be anything an integer or a string or anything
else really.
6
DATA STRUCTURES
• Pointer (link) – each linked list contains a pointer which points to address of the next
node in the linked list train.
Dynamic Size
Efficient Insertion and Deletion
No Wasted Space
Dynamic Memory Management
It only contains the address of the next node and not the previous node, so we can only
traverse in one direction only.
struct node
int data;
};
head->data=45;
head->next=NULL;
head->data=45;
7
DATA STRUCTURES
head->next=NULL;
cur->data=98;
cur->next=NULL;
head->next=cur;
cur2->data=3;
cur2->next=NULL;
head->next->next=cur;
#include<stdio.h>
#include<stdlib.h>
void main() {
struct node{
int data;
int n;
scanf("%d",&n);
head=NULL;
if(head==NULL) {
scanf("%d",&head->data);
head->next=NULL;
temp=head;
for(int i=1;i<n;i++) {
8
DATA STRUCTURES
if(new==NULL) {
printf("UNABLE TO ALLOCATE");
break; }
scanf("%d",&new->data);
new->next=NULL;
temp->next=new;
temp=new; }
//DISPLAY
if(head==NULL)
printf("List is empty");
else {
temp=head;
while(temp!=NULL) {
printf("Data=%d\n",temp->data);
temp=temp->next; }}}
//Create node
struct Node
int data;
};
position new;
new=malloc(sizeof(struct node));
if(new!=NULL) {
9
DATA STRUCTURES
new->data=X;
new->next=P->next;
P->next=new; } }
int IsEmpty(List L)
if(L->Next==NULL){
return 1; } }
if(P->Next == NULL)
return 1; }
//Find routine
position P;
P=L->next;
P=P->next; }
return P;
10
DATA STRUCTURES
position P,Temp;
P=FindPrevious(X,L);
if(!isLast(P,L))
Temp=P->Next;
P->Next=Temp->Next;
free(Temp); } }
Each node has three fields namely data field, forward link(FLINK) and backward
link(BLINK).
FLINK points to the successor node in the list whereas BLINK points to the
predecessor node.
11
DATA STRUCTURES
struct Node {
int data;
};
new=malloc(sizeof(struct Node));
if(new!=NULL) {
new->data=X;
new->Flink=P->Flink;
P->Flink->Blink=new;
new->Blink=P;
}}
Position P;
P=Find(X,L);
if(IsLast(P,L)) {
Temp=P;
P->Blink->Flink = NULL;
free(Temp); }
12
DATA STRUCTURES
else {
Temp=P;
P->Blink->Flink=P->Flink;
P->Flink->Blink=P->Blink;
Free(Temp);
Implemented as Singly linked list and Doubly linked list with or without headers.
Linked list in which the last node points to the first node.
• Polynomial ADT
• Radix Sort
• Multilist
Constant – 1,2,3 …
Variables – a,b,c …
Exponent – 5 in x^5
13
DATA STRUCTURES
struct poly
int coeff;
int power;
}*list1,*list2,*list3;
void add()
poly *ptr1,*ptr2,*new;
ptr1=list1;
ptr2=list2;
new=malloc(sizeof(struct poly));
if(ptr1->power == ptr2->power) {
new->coeff = ptr1->coeff+ptr2->coeff;
new->power=ptr1->power;
new->next=NULL;
list3=create(list3,new);
ptr1=ptr1->next;
ptr2=ptr2->next; }
14
DATA STRUCTURES
else {
if(ptr1->power>ptr2->power) {
new->coeff=ptr1->coeff;
new->power=ptr1->power;
new->next=NULL;
list3=create(list3,new);
ptr1=ptr1->next; }
else {
new->coeff=ptr2->coeff;
new->power=ptr2->power;
new->next=NULL;
list3=create(list3,new);
ptr2=ptr2->next;
} }}
void sub() {
poly *ptr1,*ptr2,*new;
ptr1=list1;
ptr2=list2;
new=malloc(sizeof(struct poly));
if(ptr1->power == ptr2->power) {
new->coeff = ptr1->coeff-ptr2->coeff;
new->power=ptr1->power;
new->next=NULL;
list3=create(list3,new);
ptr1=ptr1->next;
ptr2=ptr2->next; }
else
15
DATA STRUCTURES
if(ptr1->power>ptr2->power) {
new->coeff=ptr1->coeff;
new->power=ptr1->power;
new->next=NULL;
list3=create(list3,new);
ptr1=ptr1->next; }
else {
new->coeff=-(ptr2->coeff);
new->power=ptr2->power;
new->next=NULL;
list3=create(list3,new);
ptr2=ptr2->next;
} } }
16
DATA STRUCTURES
2.1 STACK:
Stack is a linear data structure that follows a particular order in which the operations are
performed. The order in which insertions and deletions are allowed is
called LIFO (Last In First Out) or FILO (First In Last Out) i.e. the element that is inserted in
the last is the first to come out or the element that is inserted first is the last to come out. Both
insertions and deletions to the stack are done only on one end called as top of the stack.
A Stack can be visualised as plates kept one over the other in a college canteen. Adding a
plate or removing a plate happens only on the top. The plate on the top is the first to be
removed and the plate that is put first is the last to be removed.
Example:
Push(insertion)
Pop(deletion)
17
DATA STRUCTURES
void push()
int x;
scanf("%d",&x);
if(top==size-1) {
printf("stack is full"); }
else {
top=top+1;
stack[top]=x; }
void pop() {
int y;
if(top==-1) {
printf("stack is empty");
else {
y=stack[top];
top=top-1;
}}
void push()
int value;
scanf("%d",&value);
temp->data=value;
temp->next=NULL;
18
DATA STRUCTURES
if(top==NULL)
top=temp;
else
temp->next=top;
top=temp;
void pop()
temp=top;
top=top->next;
free(temp);
Balancing symbol
19
DATA STRUCTURES
Case 2: If the stack is not empty , check the priority of the operator
If the priority of operator > operator present at the top of stack , then push operator into
stack
If the priority of operator <= operator present at the top of stack ,then pop the operator
from stack and add to postfix expression and go to step(i).
If the character is right parenthesis, then pop all the operators from the stack until it
reaches left parenthesis and add to postfix expression
Like stack, A queue is a linear data structure which follows first in first out (FIFO) principle. i.e.
the element that is inserted first is the first to come out or the element that is inserted last is
the last to come out.
A queue is a linear data structure which follows first in first out (FIFO) principle.
In queue the insertions happen at the rear end and the deletions happen at the front end of
the queue. Eg: A Queue can be visualised as customers waiting at the ticket counter. The
customer who has come first is the first to get the ticket and is the first to leave.
Implementation:
o Using Arrays
o Using Linked Lists
20
DATA STRUCTURES
Rear = 0 Front = 0
//Enqueue
void enqueue() {
int x;
scanf("%d",&x);
if(rear==size - 1) {
printf("Queue is full"); }
else {
rear=rear+1;
q[rear]=x;
if(front == -1) {
front=front+1;
}}}
21
DATA STRUCTURES
//Dequeue
void dequeue()
printf("Queue is empty"); }
else
if(front==rear) {
front=rear=-1; }
else {
front=front+1;
}}}
//enqueue
void enqueue(){
int value;
scanf("%d",&value);
new->data=value;
new->next=NULL;
if(rear==NULL) {
front=new;
22
DATA STRUCTURES
rear=new; }
else {
rear->next=new;
rear=new; } }
//dequeue
void dequeue(){
printf("Queue is Empty"); }
else if(front==rear) {
front=NULL;
rear=NULL; }
else{
temp=front;
front=front->next;
free(temp);
}}
23