+ All Categories
Home > Documents > Last meeting..Doubly Linked List InsertToFront InsertToEnd Search DeleteNode.

Last meeting..Doubly Linked List InsertToFront InsertToEnd Search DeleteNode.

Date post: 31-Dec-2015
Category:
Upload: jeffry-dalton
View: 215 times
Download: 0 times
Share this document with a friend
21
Last meeting..Doubly Linked List InsertToFront InsertToFront InsertToEnd InsertToEnd Search Search DeleteNode DeleteNode
Transcript
Page 1: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Last meeting..Doubly Linked ListLast meeting..Doubly Linked List

InsertToFrontInsertToFront InsertToEndInsertToEnd SearchSearch DeleteNodeDeleteNode

Page 2: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

InsertToFrontInsertToFront

next of new node points to headnext of new node points to head prev of node pointed by head points to prev of node pointed by head points to

new nodenew node head pointer points to new nodehead pointer points to new node

Page 3: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

InsertToEndInsertToEnd

next of tail points to new nodenext of tail points to new node prev of tail points to node pointed by tailprev of tail points to node pointed by tail tail pointer points to new nodetail pointer points to new node

Page 4: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

With Doubly Linked List, we can traverse the nodes from tail to head

With Doubly Linked List, we can traverse the nodes from tail to head

10 15

7

headptrheadptr tailptrtailptr

nullnullnullnull

datadata

prevprev nextnext

Page 5: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Create Node CodeCreate Node Codetypedef struct node * nodeptr;typedef struct node * nodeptr;

nodeptr headptr,tailptr,newnode;nodeptr headptr,tailptr,newnode;

struct node{struct node{ char * name;char * name; int age;int age; char * address;char * address; nodeptr prev, next;nodeptr prev, next;};};

node * create_node(char in_name[],int in_age,char in_address[])node * create_node(char in_name[],int in_age,char in_address[]){{ node *t = new node;node *t = new node; t->name = new char[strlen(in_name)+1];t->name = new char[strlen(in_name)+1]; t->address = new char[strlen(in_address)+1];t->address = new char[strlen(in_address)+1]; t->age = in_aget->age = in_age strcpy(t->name,in_name);strcpy(t->name,in_name); strcpy(t->address,in_address);strcpy(t->address,in_address);

t->next=NULL;t->next=NULL; t->prev=NULL;t->prev=NULL;} return t;} return t;

Page 6: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Circular Doubly Linked List FunctionsCircular Doubly Linked List Functions

Create NodeCreate Node Insert in the BeginningInsert in the Beginning Insert in the EndInsert in the End Search A NodeSearch A Node Delete A NodeDelete A Node

Page 7: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Insert a Node in the FrontInsert a Node in the Frontvoid InsertFront(nodeptr newnode)void InsertFront(nodeptr newnode)

{{

if (headptr==NULL)if (headptr==NULL)

tailptr=headptr;tailptr=headptr;

10 15

7

headptrheadptr

data newnodenewnode

newnode->next = headptr;newnode->next = headptr;

headptr->prev=newnode;headptr->prev=newnode;

headptr=newnode;}headptr=newnode;}

newnode->prev = tailptr;newnode->prev = tailptr;

tailptr->next = newnode;tailptr->next = newnode;

tailptrtailptr

Page 8: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Inserting in Front: RemindersInserting in Front: Reminders

prev of head should properly point to tailprev of head should properly point to tail head should point to the new nodehead should point to the new node

Page 9: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Insert a Node in the EndInsert a Node in the End

void InsertTail(nodeptr newnode)void InsertTail(nodeptr newnode)

{{

if (tailptr==NULL)if (tailptr==NULL)

headptr=tailptr;headptr=tailptr;

10 15

7

headptrheadptr

data

newnodenewnode

tailptrtailptr

tailptr->next =newnode;tailptr->next =newnode;

newnode->prev=tailptr; newnode->prev=tailptr;

tailptr=newnode;tailptr=newnode;

headptr->prev=tailptrheadptr->prev=tailptr

}}

Page 10: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Insert Node at End:RemindersInsert Node at End:Reminders

next of tail should point to headnext of tail should point to head prev of head should point to tailprev of head should point to tail

Page 11: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Bonus Question:Bonus Question:

What if you do not have a tail pointer? What if you do not have a tail pointer? How should you keep track the end of How should you keep track the end of the list?the list?

Page 12: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Searching…Searching…

is basically a sequential search..is basically a sequential search.. inspect each data of the node if it is the inspect each data of the node if it is the

key.key. is similar to SearchNode of Linked list is similar to SearchNode of Linked list

and Doubly Linked Listand Doubly Linked List BUT, be cautious of the HEAD and the BUT, be cautious of the HEAD and the

TAIL…this will create troubleTAIL…this will create trouble

Page 13: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Searching a nodeSearching a node

nodeptr SearchNodeFromTail(char *key)nodeptr SearchNodeFromTail(char *key)

{bool found;{bool found;

nodeptr cursor=tailptr;nodeptr cursor=tailptr;

while (cursor!=NULL && !found)while (cursor!=NULL && !found)

{ if (strcmp(cursor->name,key)==0 { if (strcmp(cursor->name,key)==0

found=true;found=true;

elseelse

cursor=cursor->prev;cursor=cursor->prev;

}; return cursor; }}; return cursor; }

nodeptr SearchNodeFromHeadchar *key)nodeptr SearchNodeFromHeadchar *key)

{bool found;{bool found;

nodeptr cursor=headptr;nodeptr cursor=headptr;

while (cursor!=NULL && !found)while (cursor!=NULL && !found)

{ if (strcmp(cursor->name,key)==0 { if (strcmp(cursor->name,key)==0

found=true;found=true;

elseelse

cursor=cursor->next;cursor=cursor->next;

}; return cursor; }}; return cursor; }

For a bonus mark of 1, can you tell if there’s For a bonus mark of 1, can you tell if there’s something wrong in these 2 functions? Discuss.something wrong in these 2 functions? Discuss.

Page 14: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Deleting a nodeDeleting a node

10 15

7

cursorcursorModify SearchNodeModify SearchNode

1.1. Declare a nodeptr prev;Declare a nodeptr prev;

2.2. Let prev follow cursor before it Let prev follow cursor before it goes to nextgoes to next

prevprev

8nullnullnullnull

Page 15: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Modify SearchNodeModify SearchNode

1.1. Declare a nodeptr prev;Declare a nodeptr prev;

2.2. Let prev follow cursor before it goes to nextLet prev follow cursor before it goes to next

3.3. If the key is found; let next of prev point to If the key is found; let next of prev point to next of cursor and previous of the next of next of cursor and previous of the next of cursor point to the previous of the cursorcursor point to the previous of the cursor

10 15

7

cursorcursor

prevprev

8nullnullnullnull

Deleting a nodeDeleting a node

Page 16: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Deleting a NodeDeleting a Node

Modify SearchNodeModify SearchNode

1.1. Declare a nodeptr prev;Declare a nodeptr prev;

2.2. Let prev follow cursor before it goes to nextLet prev follow cursor before it goes to next

3.3. If the key is found; let next of prev point to If the key is found; let next of prev point to next of cursornext of cursor

4.4. Delete node pointed by cursorDelete node pointed by cursor

10 15

cursorcursor

prevprev

87

Page 17: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Applications of Linked ListApplications of Linked List

database recordsdatabase records preliminary for advanced data structures preliminary for advanced data structures

such as binary trees, heaps,etcsuch as binary trees, heaps,etc computing power is based on linked listcomputing power is based on linked list

Page 18: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Bonus Questions:Bonus Questions:

What type of linked list will you use for What type of linked list will you use for the BUSIT bus routes (From Transport the BUSIT bus routes (From Transport Center to Sub-urbs)Center to Sub-urbs)

How about for the Orbiter Bus Route?How about for the Orbiter Bus Route?

Page 19: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

We are done with linked listsWe are done with linked lists

Next 2 weeks- Stacks and QueuesNext 2 weeks- Stacks and Queues Applicatio of stacks and queusApplicatio of stacks and queus

Page 20: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

RemindersReminders

All supervised exercises (1,2,3 and All supervised exercises (1,2,3 and 4?)for July and August are due on 164?)for July and August are due on 16thth Aug 40%Aug 40%

11stst unsupervised exercise (practical unsupervised exercise (practical laboratory test) will be on 18laboratory test) will be on 18thth Aug (Fri) Aug (Fri) 60%60%

Page 21: Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.

Recommended