0% found this document useful (0 votes)
20 views28 pages

DS Notes - M-1 & 2-3-30

The document provides an overview of data structures, focusing on linear data structures such as lists, stacks, and queues. It explains the definition, classification, operations, and applications of various data structures, including linked lists and arrays. Additionally, it covers abstract data types and specific implementations like singly and doubly linked lists, along with their operations and real-world applications.

Uploaded by

prasathg214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views28 pages

DS Notes - M-1 & 2-3-30

The document provides an overview of data structures, focusing on linear data structures such as lists, stacks, and queues. It explains the definition, classification, operations, and applications of various data structures, including linked lists and arrays. Additionally, it covers abstract data types and specific implementations like singly and doubly linked lists, along with their operations and real-world applications.

Uploaded by

prasathg214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

DATA STRUCTURES

Module I - Linear Data Structure-List

Definition of data structure-classification of data structures-data structure operations-List:


Introduction, Representation using array and linked list, Operations of Linked Lists-Types:
Singly Linked List, Doubly Linked List-Applications of Linked List: Polynomial manipulation.

1.1 DATA STRUCTURES:

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.

 “A data structure is a particular way of organizing a large amount of data more


efficiently in a computer so that any operation on that data becomes easy “
 In other words, “Data structure is a way of collecting and organizing data in such a way
that we can perform operations on these data in an effective way.”

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.

1.2 CLASSIFICATION OF DATA STRUCTURES:


Based on the organizing method of a data structure, data structures are divided into two
types:
 Primitive Data Structures (Built-In Data Structures)
 Non-primitive Data Structures (User-defined Data Structures)
Primitive data structures are those which are the predefined way of storing data by the
system. And the set of operations that can be performed on these data are also predefined.
Primitive data structures are char, int, double and float.
Non-primitive data structures are more complicated data structures and they are derived
from primitive data structures. It is used to store large and connected data. Eg. Linked
List, Tree, Graph, Stack and Queue. The non-primitive data structures are subcategorized
into two ways: Linear data structures and Non-linear data structures.

2
DATA STRUCTURES

Applications Of Data Structure


 Operating Systems
 Compiler Design
 Database Management Systems
 Statistical And Numerical Analysis
 Expert Systems
 Network Analysis
Real life examples:

o Stack data structure is used in implementing redo and undo feature.


o Array data structure is used to store an image as a bitmap
o Graph data structure is used to store the friendship information on a social
networking site such as facebook.

1.3 DATA STRUCTURE OPERATIONS:


The data appearing in data structures are processed by means of certain operations. The
following four operations play a major role in this text.
1. Traversing: accessing each record/node exactly once so that certain items in the
record may be processed. (This accessing and processing is sometimes called
“visiting” the record.)
2. Searching: Finding the location of the desired node with a given key value, or
finding the locations of all such nodes which satisfy one or more conditions.
3. Inserting: Adding a new node/record to the structure.
4. Deleting: Removing a node/record from the structure.

Abstract Data Types :

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.

1.4 List ADT:

A list is any information displayed or organized in a logical or linear formation. In


computer science, a list or sequence is an abstract data type that represents a countable
number of ordered values, where the same value may occur more than once.

A list contains elements of same type arranged in sequential order and following
operations can be performed on the list.

General form : A1, A2, A3, … .. , AN

Implementation of List:

The List can be implemented in two ways:

• Using Arrays

• Using Pointer or Linked list

1.4.1 Array Implementation of List:

• Array is a collection of specific number of data stored in a consecutive memory


location.

• Insertion and Deletion Operation are expensive as it requires more data movement.

• Find and Printlist operations takes constant time.

• 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

• Insertion of data in list

• Deletion of data from list

4
DATA STRUCTURES

• Display all data in list

• Searching of data in list

Routine for operations of array:

//Creation of List

void create() {

int n, list[20];

printf("Enter no of elemts to be added");

scanf("%d",&n);

printf("\n Enter the array elements:");

for(int i=0;i<n;i++) {

scanf("%d",&list[i]);

} }

//Insertion operation

void insert() {

int i,data,pos;

create();

printf("Enter the data to be inserted \t");

scanf("%d",&data);

printf("Enter the position to be inserted");

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

Searching and deletion

void search() {

printf("Enter element to be deleted");

scanf("%d",&d);

for(int i=0;i<n;i++){

if(d==list[i]) {

p=i;

found=1;

break;

} } }

Searching and deletion

void delete() {

if(found){

for(int i=p; i<n-1;i++)

list[i] = list[i+1];

n--;

printf("\n The array after deletion is : ");

for(int i=0;i<n;i++)

printf("\n list[%d] = %d", i, list[i]); }

else {

printf("Element not found");

}}

1.4.2 Linked List:


A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations.

A linked list has the following components,

• 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.

Why we use Linked List in Data Structure?

 Dynamic Size
 Efficient Insertion and Deletion
 No Wasted Space
 Dynamic Memory Management

Types of Linked link:

1. Singly linked list

2. Doubly linked list

3. Circular linked list

1.4.2.1 Singly-linked list:

It only contains the address of the next node and not the previous node, so we can only
traverse in one direction only.

Structure of Singly Linked List:

struct node

int data;

struct node* next;

};

Creation of Single Node:

struct node *head=NULL;

head=(struct node*)malloc(sizeof(struct node));

head->data=45;

head->next=NULL;

Creating Singly Linked List:

struct Node *head=NULL;

head=(struct node*)malloc(sizeof(struct node));

head->data=45;

7
DATA STRUCTURES

head->next=NULL;

struct node *cur=malloc(sizeof(struct node));

cur->data=98;

cur->next=NULL;

head->next=cur;

struct node *cur2=malloc(sizeof(struct node));

cur2->data=3;

cur2->next=NULL;

head->next->next=cur;

Read n elements in singly linked list:

#include<stdio.h>

#include<stdlib.h>

void main() {

struct node{

int data;

struct node *next; };

int n;

printf("ENTER NUMBER OF NODE: ");

scanf("%d",&n);

struct node *head,*new;

head=NULL;

head=(struct node*) malloc(sizeof(struct node));

if(head==NULL) {

printf("UNABLE to ALLOCATE MEM"); exit(0); }

printf("Enter data of node1:\t");

scanf("%d",&head->data);

head->next=NULL;

struct node *temp;

temp=head;

//READ n data using for loop

for(int i=1;i<n;i++) {

8
DATA STRUCTURES

new=(struct node*) malloc(sizeof(struct node));

if(new==NULL) {

printf("UNABLE TO ALLOCATE");

break; }

printf("Enter data of node %d\t",i+1);

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; }}}

OPERATIONS IN SINGLY LINKED LIST:

//Create node

struct Node

int data;

struct Node *Next;

};

typedef struct Node *List;

typedef struct Node *Position;

//Routine to insert an element in the list

void Insert(int X,List L,Position P) {

position new;

new=malloc(sizeof(struct node));

if(new!=NULL) {

9
DATA STRUCTURES

new->data=X;

new->next=P->next;

P->next=new; } }

//Routine to check whether the list is empty:

int IsEmpty(List L)

if(L->Next==NULL){

return 1; } }

//Routine to check whether the current position is last:

int IsLast(Position P,List L){

if(P->Next == NULL)

return 1; }

//Find routine

position Find(int X, List L) {

position P;

P=L->next;

while(P!=NULL && P->data!=X) {

P=P->next; }

return P;

10
DATA STRUCTURES

//Delete an element from the list

void Del(int X,List L)

position P,Temp;

P=FindPrevious(X,L);

if(!isLast(P,L))

Temp=P->Next;

P->Next=Temp->Next;

free(Temp); } }

1.4.2.2 Doubly linked list:

 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

Declaration of doubly linked list:

struct Node {

int data;

struct Node *Flink;

struct Node *Blink;

};

Routine to insert an element in a Doubly Linked List:

void insert(int X, List L,Position P) {

struct Node *new;

new=malloc(sizeof(struct Node));

if(new!=NULL) {

new->data=X;

new->Flink=P->Flink;

P->Flink->Blink=new;

new->Blink=P;

}}

//Routine to delete an element

void Del(int X,List L) {

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);

1.4.2.3 Circular Linked List

 The pointer of the last node points to the first node.

 Implemented as Singly linked list and Doubly linked list with or without headers.

Singly Linked Circular List

 Linked list in which the last node points to the first node.

1.5 Applications of Linked List:

• Polynomial ADT

• Radix Sort

• Multilist

1.5.1 POLYNOMIAL – ADT:

• A Polynomial is made up of two terms namely poly(meaning many) and


Nominal(meaning terms).

• A polynomial is defined as an expression which is composed of variables, constant,


and exponents that are combined using operation such as addition , subtraction,
multiplication and division.

Constant – 1,2,3 …

Variables – a,b,c …

Exponent – 5 in x^5

13
DATA STRUCTURES

Declaration for Linked list implementation of polynomial ADT

struct poly

int coeff;

int power;

struct poly *next;

}*list1,*list2,*list3;

ADDITION OF TWO POLYNOMIALS

void add()

poly *ptr1,*ptr2,*new;

ptr1=list1;

ptr2=list2;

while(ptr1!=NULL && ptr2!=NULL)

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;

} }}

SUBTRACTION OF TWO POLYNOMIALS:

void sub() {

poly *ptr1,*ptr2,*new;

ptr1=list1;

ptr2=list2;

while(ptr1!=NULL && ptr2!=NULL)

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

Module II - Linear Data Structure - Stack, Queue

Stack: Definition, Array representation of stack, Linked representation of stack-Applications of


Stack: Evaluation of a Postfix Expression, Infix Expressions into Postfix Expressions. Queue:
Definition, Array representation of Queue, Linked representation of Queue-Applications of
Queue.

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:

2.2 Operation on stack:

Two fundamental operation performed on the stack

 Push(insertion)
 Pop(deletion)

Implementation of stack can be done in two ways:

 Array implementation of stack


 Linked list implementation of stack

17
DATA STRUCTURES

2.2.1 Array implementation of stack:

void push()

int x;

printf("enter the elements");

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;

printf("the deleted element is %d",y);

}}

2.2.2 Linked List Implementation of stack:

void push()

int value;

printf("enter the value to be created");

scanf("%d",&value);

temp=(struct node*)malloc(sizeof(struct node));

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);

2.3 Stack application :

 Infix to postfix expression

 Evaluation of postfix expression

 Balancing symbol

Evaluation of postfix expression (Algorithm)

o If character is an operand ,then push into stack


o If the character is an operator
 Pop the top two operands from the stack and push the result back into
stack
o After reading all the character from the postfix expression stack will be having
only the value which is result

Infix to postfix expression(Algorithm):

1. If the character is left parenthesis , push to the stack

2.If the character is an operand , add to the postfix expression

3.If the character is an operator ,check whether stack is empty

Case 1: If the stack is empty , push operator into the stack

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

2.4 QUEUE ADT:

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.

Queue having two pointers:

 Rear (Insertion performed)

 Front (Deletion performed)

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.

The major operations of the queue are :

o enqueue() - Inserts an element into the queue.


o dequeue() - Removes an element from the queue
o size() - Returns the size of the queue i.e the number of elements in the queue
o isEmpty() - Returns true if the queue is empty , else returns false

Implementation:

There are two ways of implementing queues :

o Using Arrays
o Using Linked Lists

20
DATA STRUCTURES

Array based implementation

Rear = 0 Front = 0

Array Implementation of Queue:

//Enqueue

void enqueue() {

int x;

printf("Enter the element:");

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()

if(front==-1 && rear==-1) {

printf("Queue is empty"); }

else

printf("The deleted element %d",q[front]);

if(front==rear) {

front=rear=-1; }

else {

front=front+1;

}}}

2.5. Linked List Based Implementation:

Linked List Implementation of Queue:

//enqueue

void enqueue(){

int value;

printf("Enter the value");

scanf("%d",&value);

new=(struct node*) malloc(sizeof(struct node));

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(){

if(front==NULL && rear==NULL){

printf("Queue is Empty"); }

else if(front==rear) {

printf("The deleted elements in queue %d",front->data);

front=NULL;

rear=NULL; }

else{

temp=front;

front=front->next;

printf("The deleted elements from the queue is %d",temp->data);

free(temp);

}}

23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy