Date post: | 31-Dec-2015 |
Category: |
Documents |
Upload: | jeffry-dalton |
View: | 215 times |
Download: | 0 times |
Last meeting..Doubly Linked ListLast meeting..Doubly Linked List
InsertToFrontInsertToFront InsertToEndInsertToEnd SearchSearch DeleteNodeDeleteNode
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
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
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
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;
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
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
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
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
}}
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
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?
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
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.
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
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
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
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
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?
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
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%