Download presentation
Presentation is loading. Please wait.
Published byJeffry Dalton Modified over 9 years ago
1
Last meeting..Doubly Linked List InsertToFront InsertToEnd Search DeleteNode
2
InsertToFront next of new node points to head prev of node pointed by head points to new node head pointer points to new node
3
InsertToEnd next of tail points to new node prev of tail points to node pointed by tail tail pointer points to new node
4
With Doubly Linked List, we can traverse the nodes from tail to head 10 1515 7 headptr tailptr null null data prevnext
5
Create Node Code typedef struct node * nodeptr; nodeptr headptr,tailptr,newnode; 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 *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_age t->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;
6
Circular Doubly Linked List Functions Create Node Insert in the Beginning Insert in the End Search A Node Delete A Node
7
Insert a Node in the Front void InsertFront(nodeptr newnode) { if (headptr==NULL) tailptr=headptr; tailptr=headptr; 10 1515 7 headptr data newnode newnode->next = headptr; headptr->prev=newnode; headptr=newnode;} newnode->prev = tailptr; tailptr->next = newnode; tailptr
8
Inserting in Front: Reminders prev of head should properly point to tail head should point to the new node
9
Insert a Node in the End void InsertTail(nodeptr newnode) { if (tailptr==NULL) headptr=tailptr; headptr=tailptr; 10 1515 7 headptr data newnode tailptr tailptr->next =newnode; newnode->prev=tailptr; tailptr=newnode;headptr->prev=tailptr}
10
Insert Node at End:Reminders next of tail should point to head prev of head should point to tail
11
Bonus Question: What if you do not have a tail pointer? How should you keep track the end of the list?
12
Searching… is basically a sequential search.. inspect each data of the node if it is the key. is similar to SearchNode of Linked list and Doubly Linked List BUT, be cautious of the HEAD and the TAIL…this will create trouble
13
Searching a node nodeptr SearchNodeFromTail(char *key) {bool found; nodeptr cursor=tailptr; while (cursor!=NULL && !found) { if (strcmp(cursor->name,key)==0 found=true; found=true;else cursor=cursor->prev; cursor=cursor->prev; }; return cursor; } nodeptr SearchNodeFromHeadchar *key) {bool found; nodeptr cursor=headptr; while (cursor!=NULL && !found) { if (strcmp(cursor->name,key)==0 found=true; found=true;else cursor=cursor->next; cursor=cursor->next; }; return cursor; } For a bonus mark of 1, can you tell if there’s something wrong in these 2 functions? Discuss.
14
Deleting a node 10 1515 7 cursor Modify SearchNode 1. Declare a nodeptr prev; 2. Let prev follow cursor before it goes to next prev 8 null null
15
Modify SearchNode 1. Declare a nodeptr prev; 2. Let prev follow cursor before it goes to next 3. If the key is found; let next of prev point to next of cursor and previous of the next of cursor point to the previous of the cursor 10 1515 7 cursor prev 8 null null Deleting a node
16
Deleting a Node Modify SearchNode 1. Declare a nodeptr prev; 2. Let prev follow cursor before it goes to next 3. If the key is found; let next of prev point to next of cursor 4. Delete node pointed by cursor 10 1515 cursor prev 87
17
Applications of Linked List database records preliminary for advanced data structures such as binary trees, heaps,etc computing power is based on linked list
18
Bonus Questions: What type of linked list will you use for the BUSIT bus routes (From Transport Center to Sub-urbs) How about for the Orbiter Bus Route?
19
We are done with linked lists Next 2 weeks- Stacks and Queues Applicatio of stacks and queus
20
Reminders All supervised exercises (1,2,3 and 4?)for July and August are due on 16 th Aug 40% 1 st unsupervised exercise (practical laboratory test) will be on 18 th Aug (Fri) 60%
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.