Download Linked list

Document related concepts

Array data structure wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

Linked list wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Presented by : Preeti Banswal
What is data structure?
• A data structure is a way of
organizing data that considers not
only the items stored, but also their
relationship to each other.
• Advance knowledge about the
relationship between data items
allows designing of efficient
algorithms for the manipulation of
data.
Areas in which data structures are
applied extensively:
 Operating System.
 Database Management System,.
 Statistical analysis package,
 Numerical Analysis,
 Compiler Design.
 Graphics.
 Artificial Intelligence.
 Simulation.
Data
structure
NonPrimitive
Primitive
int
char
float
Linear
Stack
Queue
Nonlinear
Linked
list
Trees
Graphs
Contents :
 Basic Concepts
 Arrays and Structures.
 Stacks and Queue.
 Linked Lists.
 Trees, Binary trees.
 Graphs
 Efficient Binary Search Trees.
Basic Concepts :
Pointers and dynamic memory allocation.
 Algorithm Specification.
 Data abstraction.
 Performance analysis.
 Performance measurement
Pointers:
 Pointers are fundamental to C .
 C provides extensive support for pointer .
& : Address Operator
* : Dereferencing or Indirection operator
• Example :
int i , *pi;
pi=&i;
Pointer Operations in C
 Creation
& variable
Returns variable’s memory address
 Dereference
• pointer
Returns contents stored at address
 Indirect assignment
* pointer = val
Stores value at address
 Assignment
pointer = ptr
Stores pointer in another variable
Some pointers:
 Generic Pointer : void pointer in C is known as
generic pointer.
• Generic pointer is a pointer which can point type of
data.
• NULL pointer is a pointer which is pointing to
nothing.
int *ptr=NULL
 Wild Pointer : A pointer in C which has not
been initialized is known as wild pointer.
Dangling pointers :
Dangling pointers in computer
programming are pointers that do not point to
a valid object of the appropriate type.
Memory Allocation

Memory allocation is a process by which computer programs and
services are assigned with physical or virtual memory space.
 Memory allocation has two core types,
1. Static Memory Allocation: The program is
allocated memory at compile time.
2.Dynamic Memory Allocation: The programs are
allocated with memory at run time.
Difference between static & dynamic
memory allocation:
Static Memory Allocation
Dynamic Memory Allocation
Memory is allocated before the
execution of the program begins.
(During Compilation)
Memory is allocated during the
execution of the program.
No memory allocation or deallocation
actions are performed during
Execution.
Memory Bindings are established and
destroyed during the Execution.
Variables remain permanently
allocated.
Allocated only when program unit is
active.
Implemented using stacks and heaps
Implemented using data segments.
Pointer is needed to accessing
variables.
No need of Dynamically allocated
pointers.
Faster execution than Dynamic
Slower execution than static
More memory Space required.
Less Memory space required.
Pointers in C
14
Dynamic memory functions:
malloc( )
Allocates specified number of bytes
Syntax : void * malloc (size_t size);
calloc( )
Allocates specified number of bytes and initializes to zero.
• Syntax : void *calloc(size_t nitems, size_t size)
• where , nitems -- This is the number of elements to be allocated.
size -- This is the size of elements
 realloc( )
Increases or decreases size of specified block of memory.
Syntax : void *realloc(void *ptr, size_t size)
 free( )
De-allocate memory, returns specified block of memory back
to the system.
Data type & Abstract Data
type:
A data type is a collection of objects and a set
of operations that act on those object.
Eg: int , char , float.
 An abstract data type (ADT) is a data type
that is organized in such a way that the
specification of objects is separated from
representation of objects and the
implementation of the operations.
Algorithm Specification:
 An algorithm is finite set of instruction that,
if followed , accomplishes a particular task.
 Criteria that every algorithm should follow:
1. Input
2. Output
3. Definiteness
4. Finiteness
5. Effectiveness
Performance Analysis :
• Space complexity : Space complexity of a program
is the amount of memory that it needs to run to
completion.
• 1: Fixed space requirement.
• 2: Variable space requirement.
S(P) = c+ Sp(I)
• Time complexity : The time complexity of a program
is the amount of computer time that it needs to run to
completion.
• The recursive function has lower step count
then its iterative counterpart.
• Recursive function typically run slower than
the iterative version & takes more time than
those of the iterative function.
• Recursive function also uses more memory
space for its each call.
• For space complexity, iterative function is
better than recursive function.
• For time complexity , recursive function is
better than iterative function.
Arrays and Structures
• Arrays: An Array is a finite collection of similar
elements stored in adjacent memory locations.
Arrays
One-dimensional
Multidimensional
Array
Array
One dimensional array
Multi-dimensional Array:
Representation of multidimensional
arrays :
 Consider matrix of N*N ,each entry of which is floating
number.
Float m[N][N] ;
 An element of this matrix m, say
accessed as,
m i,j
will be
m[i-1][j-1];
 we can also define 4-dimensional integers
array as follows,
int k[N][M][K][L];
Structure
• A Structure is a collection of data of different type.
• struct is used .
• eg:
struct {
char name[10];
int age;
float salary;
} person;
• Notice the use of the ( . ) as the structure member
operator. We use this operator to select a particular
member of the structure .
• We can create our own structure data types
by using the typedef statement below:
typedef struct {
char name[10];
int age;
float salary;
} person;
person p1 , p2 ;
Unions
• A union declaration is similar to a structure ,
but the field of a union must share their memory space.
• This means that only one field of the union
is active at any given time..
• eg: union abc
{
char c;
long l;
};
Self-Referential Structure
• A self-referential Structure is one in which one or
more of its components is a pointer to itself .
• self-referential structure usually require dynamic
storage management routines (malloc and free)
to explicitly obtain and release memory.
eg:
typedef struct {
char data;
struct list *link ;
} list;
Polynomial :
• Structure:
MAX_TERMS 100 /*size of terms array*/
typedef struct
{
float coef;
int expon ;
} polynomial;
polynomial terms[MAX_TERMS];
int avail=0;
Polynomial :
• Array representation:
ex: A(x)= 2x^1000 + 1
B(x)=x^4+10x^3+3x^2+1
finish A
startA
startB
finish B
1 1 10 3 1
Coef: 2
Expon: 1000 0 4 3 2 0
avail
Sparse matrix
Structure :
Define MAX_TERMS 101 /*maximum number of
term +1*/
typedef struct
{
int cols;
int rows;
int value;
}terms;
Term a[MAX_TERMS];
Stacks :
 A stack is a data linear data structure in which
addition of new element or deletion of an
existing element always takes place at the
same end. This end is
called as top of the stack.
 Stack is also called as
last–in-first-out (LIFO).
• When an item is added to a stack, the
operation is called as push.
• When an item is removed from the stack, the
operation is called as pop .
Structure for stack:
• # define MAX_STACK_SIZE 100
/* maximum stack size*/
typedef struct
{
int key;
/*other field */
} elements;
element stack[ MAX_STACK_SIZE];
int top= -1;
Stack empty condition :
top < 0;
Stack full condition :
top >= MAX_STACK_SIZE - 1
int stack[5] , top = -1;
void push( )
{
int item;
if (top<=4)
{
printf(“\n Enter the number”);
scanf(“%d”, &item);
top=top+1;
stack [top] = item;
}
else
{
printf(“\n stack overflow”);
}
}
int stack [5],top;
void pop ( )
{
int item;
if (top>=0)
{
item = stack [top];
top=top-1;
printf("\n Number deleted is = %d ", item);
}
else
{
printf("\n stack is empty");
}
}
Application of stack
Direct applications
Page-visited history in a Web browser
Undo sequence in a text editor
Saving local variables when one function
calls another, and this one calls another,
and so on.
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
Queue
 Queue is a linear data structure that permits the
insertion of new element at one end and
deletion of an element at the other end.
 The end at which the deletion of an element takes
place is called as front.
 Which insertion of new element can take place is
called as rear.
 Queue is also called as first-in-first-out (FIFO).
BACK
FRONT
DEQUEUE
ENQUEUE
Structure of Queue:
• # define MAX_QUEUE_SIZE 100
/* maximum Queue size*/
typedef struct
{
int key;
/*other field */
} elements;
element queue[ MAX_QUEUE_SIZE];
int rear= -1;
int front= -1;
• Queue empty condition :
front == rear
•
Queue full condition:
rear == MAX_QUEUE_SIZE -1
int queue [5], front = -1, rear = -1 ;
Void queue ( )
{
int item ;
if (rear < 4)
{
printf(“\n Enter the number ”);
scanf(“%d ” & item );
if (front = = -1 )
{
front = 0 ;
rear = 0;
}
int queue [5] , front , rear ;
void delete ( )
{
int item ;
If ( front ! = -1)
{
item = queue [front];
if (front = = rear )
{
front = -1;
rear = -1 ;
}
front = front +1 ;
}
printf (“\n Number deleted is = %d “, item );
}
else
{
printf( “ Queue is empty”);
}
}
Application of Queue
1) Serving requests of a single shared resource (printer,
disk, CPU),transferring data asynchronously (data not
necessarily received at same rate as sent) between two
processes (IO buffers), e.g., pipes, file IO, sockets.
2) Call center phone systems will use a queue to hold people
in line until a service representative is free.
3) When a resource is shared among multiple
consumers. Examples include CPU scheduling, Disk
Scheduling.
Expression using stack:
???
Disadvantage of Queue :
• In simple queue , if rear is present at the
maxsize of queue , though there may empty
slots at beginning or middle of queue,
queue will be reported as full….
the solution here is to use
circular queue
Circular queue:
Disadvantages of queue
representation using static array:
• Drawback is that here we can allocate memory
statically , so if array is not fully utilized ,
memory will be wasted and once array declared
, we can not increase size of array …. So
element in queue must be inserted within
given array….
Drawbacks of sequential
representation:
 Drawbacks of sequential i.e. array representation
is that it is completely static representation…..
 We can not increase or decrease size of array at
runtime..
 In sequential representation, elements are
stored in adjacent memory locations where as in
dynamic representation(linked list), every node is
holding address of next node…
Linked list:



Linked list is a very common data
structure often used to store similar
data in memory.
This memory is randomly selected by
the compiler.
The order of the elements is maintained
by explicit links between them.
Node
Data
Link
Structure of linked list :
• typedef struct listNode *listpointer;
typedef struct
{
int data;
listPointer link;
} listNode;
Types of linked list
 Singly linked list
 Doubly linked list
 Circular linked list
ADVANTAGES OF LINKED REPRESENTATION
OVER SEQUENTIAL REPRESENTATION
1)In sequential representation the memory is allocated
sequentially whereas in linked representation memory is
allocated randomly.
2)In sequential representation we does not require address
of next element where as in linked representation to access
list elements in the correct order,with each element we store
the address of the next element in that list…
etc.
3) In array, element are stored in adjacent
memory locations whereas linked list every
node allocates the memory wherever
available , linked list is also called scattered
family.
4) In linked list , every node knows where the
next node is present in memory with the help
of link, it is not possible in array…
Polynomial using linked list
Structure for node of polynomial:
• typedef struct polyNode *polyPointer
typedef struct
{
int coef;
int expon;
polyPointer link;
} polyNode;
polyPointer a, b;
Sparse Matrix
• Sparse matrix is that matrix which has more
number of zero’s or can also be said as that
matrix which consist of less number of nonzero numbers.
REPRESENTATION OF SPARSE MATRIX
HEADER
NODE
ELEMENT
NODE
Doubly linked list :
Structure for doubly linked list
• typedef struct node *nodePointer;
typedef struct
{
nodePointer llink;
element data;
nodePointer rlink;
} node;
Circular linked list:
Trees:
• A tree is a finite set of one or more node such
that,
1: there is a specially designated node called
root.
2: the remaining nodes are partitioned into
n>=0 disjoint sets T0 ,T1,T2,……….Tn-1
where T0 ,T1,T2,………Tn-1 Are called the subtrees
of the root
55
typedef struct node
{
int data ;
struct node* left;
struct node* right;
}
node;
Representation of tree:
 List representation
 Left child right sibling representation
 Representation as degree two tree.
Basic terminology
• Root –
•
•
•
•
The top node in a tree.
Parent – The converse notion of child.
Siblings – Nodes with the same parent.
Descendant – a node reachable by
repeated proceeding from parent to
child.
Ancestor – a node reachable by repeated
proceeding from child to parent.
• Leaf – a node with no children.
• Internal node – a node with at least one
child.
• External node – a node with no children.
• Degree – number of sub trees of a node.
• Edge – connection between one node to
another.
• Path – a sequence of nodes and edges
connecting a node with a descendant.
• Level – The level of a node is defined
by 1 + the number of connections
between the node and the root.
• Height – The height of a node is the
number of edges on the longest
downward path between the node and
a leaf.
• Forest – A forest is a set of n ≥ 0
disjoint trees.
Binary Search Tree
In computer science, a binary search tree (BST) or
ordered binary tree is a node-based binary tree data
structure which has the following properties:
• The left subtree of a node contains only nodes with
keys less than the node's key.
• The right subtree of a node contains only nodes with
keys greater than the node's key.
• Both the left and right subtrees must also be binary
search trees.
Representation of binary tree:
 Linked list representation.
 Array representation.
Difference between binary tree and
binary search tree
• Binary Tree
In short, a binary tree is a •
tree where each node has
up to two leaves. In a
binary tree, a left child
node and a right child
node contain values which
can be either greater, less,
or equal to parent node.
• Binary Search Tree
In binary search tree, the
left child contains nodes
with values less than the
parent node and where
the right child only
contains nodes with values
greater than the parent
node. There must be no
duplicate nodes.
Traversal
Graphs
• A graph consists of two sets v and e where ,
v is finite , non-empty set of vertices and e
is set of pairs of vertices..
• The pairs of vertices are called edges.
Graphs can be of two types:
• Undirected graph
• Directed graph
Representation of Graphs :
 Adjacency matrix
 Adjacency lists
 Adjacency multilists
 Weighted list
Priority queue:
• A priority queue is collection of element such
that each element has an associated priority.
There are two types of priority queue ,
1: Single ended priority queue
2: Double ended priority queue
Heaps :
• Binomial heap
1. Max Binomial heap
2. Min binomial heap
• Fibonacci heap
1. Max Fibonacci heap
2. Min Fibonacci heap
• Pairing heap
1. Max Pairing heap
2.Min pairing heap
AVL Tree
• AVL tree definition
– a binary tree in which the maximum difference
in the height of any node’s right and left subtrees is 1 (called the balance factor)
• balance factor = height(right) – height(left)
• AVL trees are usually not perfectly balanced
– however, the biggest difference in any two
branch lengths will be no more than one level
AVL Tree
-1
0
0
0
0
0
AVL Tree
-1
1
0
0
AVL Tree
Definition: Red-Black Tree
• Each node must have exactly two children. For each child
that is lacking, you create a fake black one.
10
13
5
needs two fake
children →
← needs two fake children
7
13
6
← needs one fake child
9
← needs two fake children
Balanced trees: Red-black trees
Definition: Red-Black Tree
• Each node must have exactly two children. For each child
that is lacking, you create a fake black one.
10
13
5
7
13
6
9
Balanced trees: Red-black trees