data structures2

34
CS1301 Data Structures

Upload: parthipan-parthi

Post on 28-Jun-2015

59 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Data structures2

CS1301 Data Structures

Page 2: Data structures2

Unit I INTRODUCTION Introduction Data structure Linked DS Succinct DS Implicit DS Compressed DS Search DS Static and Dynamic DS Persistent DS Concurrent DS Arrays Recursions

Page 3: Data structures2

Data structure

• a particular way of storing and organizing data in a computer so that it can be used efficiently.

Page 4: Data structures2

Linked DS• is a data structure which consists of a set of data

records (nodes) linked together and organized by references (links or pointers)

• In linked data structures, the links are usually treated as special data types that can only be compared for equality

• a structure contains a pointer to another instance of the same structure or different structure

• Linking can be done in two ways - Using dynamic allocation and using array index linking

• include linked lists, search trees, expression trees

Page 5: Data structures2

Linked DS

• Advantages against arrays–more flexibility– saving wasted memory (built

dynamically)– the reference to each node gives us the

information where to find out the next one

–Memory can be utilized more efficiently

Page 6: Data structures2

Linked DS

• General disadvantages– follow multiple pointers so element

access time varies–may also use more memory

Page 7: Data structures2

Linked LIST

• Consists of series of node• Each node of the list contains

• the data item• a pointer to the next node

• Flexible space use• Dynamically allocate space for each element

as needed• Include a pointer to the next item

Object

Data Next

Page 8: Data structures2

Linked LIST

• Types of Linked List– Singly Linked List– Doubly Linked List– Circular Linked List

Page 9: Data structures2

SINGLY Linked LIST

• Each node contains only one link field pointing to the next node in the list

700 1000 800

550 700 1000 80010 20 30 40

Header

Page 10: Data structures2

SINGLY Linked LISTCollection structure has a pointer to the list

headInitially NULL

Add first itemAllocate space for nodeSet its data pointer to objectSet Next to NULLSet Head to point to new node

Data Next

object

Head

Collectionnode

Page 11: Data structures2

SINGLY Linked LISTAdd second item

Allocate space for nodeSet its data pointer to objectSet Next to current HeadSet Head to point to new node

Data Next

object

Head

Collection

nodeData Next

object2

node

Page 12: Data structures2

SINGLY Linked LIST• Declaration for Linked ListStruct node;typedef struct Node *Listtypedef struct Node *Positionint IsLast (List L);int IsEmpty(List L);position Find(int x, List L);void delete(int x, List L);Struct node{

int element;position next;

};

Page 13: Data structures2

SINGLY Linked LIST• Routine to insert an element in the listvoid insert(int x, List L, Position P) // insert after position P{Position Newnode;Newnode = malloc(size of(Struct Node));If(Newnode !=NULL){

Newnode Element =X;Newnode Next =PNext;P Next = Newnode;

}}

Page 14: Data structures2

To insert an element in the list

Page 15: Data structures2

SINGLY Linked LIST

• Routine to check whether list is empty

int IsEmpty(List L) // returns 1 if L is empty{

if (L Next == NULL)return(1);

}

Page 16: Data structures2

SINGLY Linked LIST

• Routine to check whether current position is last

int IsLast(position p,List L) // returns 1 if P is the last position in L

{if (P Next == NULL)return(1);

}

Page 17: Data structures2

SINGLY Linked LIST• Find Routine position Find(int x,List L){//Returns the position of x in L, null if x is not foundPosition P;P= L Next;

while(P!= NULL && PElement!= X)P=PNextreturn P;

}

Page 18: Data structures2

SINGLY Linked LIST• Find Previous Routine position Findprevious(int x,List L){//Returns the position of the predecessorPosition p;P=L

while(P Next!= NULL && PNextElement!= X)P=PNextreturn P;

}

Page 19: Data structures2

SINGLY Linked LIST• Find Next Routine position FindNext(int x,List L){//Returns the position of its successorP=L Next

while(P Next!= NULL && PElement!= X)P=PNextreturn PNext;

}

Page 20: Data structures2

SINGLY Linked LIST• Routine to delete an element from the listVoid Delete(int x, List L){// Delete the first occurrence of X from the list

position P, temp;P= Findprevious(x,L);If(!IsLast(P,L)){

Temp=P Next;PNext = Temp Nextfree(temp);

}}

Page 21: Data structures2

SINGLY Linked LIST• Routine to delete the listVoid DeleteList(List L){

position P, Temp;P= LNext ; LNext = NULL;while(P!=NULL){

Temp=P Next;free(P);P=Temp;

}}

Page 22: Data structures2

DoUBLY Linked LIST

• Each node has three fields– Data field– Forward Link(FLINK)– Backward link(BLINK)

• FLINK points to the successor node in the list

• BLINK points to the predecessor nodeBLINK Data FLINK

Page 23: Data structures2

Doubly Linked LIST

L10 20

Header

30

Structure DeclarationStruct Node{Int element;Struct Node *FLINK;Struct Node *BLINK;}

DHIKHI
Page 24: Data structures2

DoUBLY Linked LISTRoutine to insert an element in a doubly linked listVoid insert(int x, list l, position p){

struct Node *Newnode;Newnode= malloc(size of(Struct Node));If (Newnode !=NULL){Newnode Element =x;Newnode Flink = p Flink;P flink blink =Newnode;P Flink = Newnode;Newnode Blink = p;}

}

Page 25: Data structures2

DoUBLY Linked LIST

Page 26: Data structures2

DoUBLY Linked LIST

Page 27: Data structures2

DoUBLY Linked LIST

• Advantages– Deletion operation is easier– Finding predecessor & successor of a

node is easier

• Disadvantages–More memory space required since it

has two pointers

Page 28: Data structures2

CIRCULAR Linked LIST

• Pointer of last node points to the first node

• Types–Singly Linked Circular List–Doubly Linked circular List

Page 29: Data structures2

CIRCULAR Linked LIST

• Singly Linked Circular List–Last node of the list points to the first node

Page 30: Data structures2

CIRCULAR Linked LIST

• Doubly Linked Circular List– Doubly linked list– Forward link of last node points to the first node– Backward link of the first node points to the last

node of the list

Page 31: Data structures2

CIRCULAR Linked LIST

• Advantages–Allows to traverse the list starting at

any point–Allows quick access to the first and

last records–Circular doubly linked list allows the

list to traverse in either directions.

Page 32: Data structures2

POINTERS and arrays• Special variables which contain the address of

another memory locationint arr[4] = {2,4,6,8};

• arr acts as constant pointer pointing to first element

• arr= &arr[0]=100 arr[0] arr[1] arr[2] arr[3]

100 102 104 106

• & is the address operator, represents address of the variable

• %u – obtaining the address

2 4 6 8

Page 33: Data structures2

POINTERS

main(){

int a=2;printf(“value of a=%d”,a); printf(“addressof a=%u”,&a);

} a – variable namevalue of a or value at address 1000

1000 - address

2

Page 34: Data structures2

• * operator is the value of at the address operator• Pointer variable declaration - * should preced

variable nameint *b;b=&a;

• b is the variable which contains the address of variable a as its value

• Pointer variable that stores the address of a pointer variableint a=2; int *b;int **c; b=&a; c=&b;

• C has been declared as pointer to pointer variable which contains the address of pointer variable b