data structures2

Post on 28-Jun-2015

60 Views

Category:

Engineering

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS1301 Data Structures

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

Data structure

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

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

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

Linked DS

• General disadvantages– follow multiple pointers so element

access time varies–may also use more memory

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

Linked LIST

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

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

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

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

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;

};

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;

}}

To insert an element in the list

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

}

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

}

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;

}

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;

}

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;

}

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

}}

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;

}}

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

Doubly Linked LIST

L10 20

Header

30

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

DHIKHI

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

}

DoUBLY Linked LIST

DoUBLY Linked LIST

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

CIRCULAR Linked LIST

• Pointer of last node points to the first node

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

CIRCULAR Linked LIST

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

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

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.

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

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

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

top related