Last meeting..Doubly Linked List InsertToFront InsertToEnd Search DeleteNode
InsertToFront next of new node points to head prev of node pointed by head points to new node head pointer points to new node
InsertToEnd next of tail points to new node prev of tail points to node pointed by tail tail pointer points to new node
With Doubly Linked List, we can traverse the nodes from tail to head headptr tailptr null null data prevnext
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;
Circular Doubly Linked List Functions Create Node Insert in the Beginning Insert in the End Search A Node Delete A Node
Insert a Node in the Front void InsertFront(nodeptr newnode) { if (headptr==NULL) tailptr=headptr; tailptr=headptr; headptr data newnode newnode->next = headptr; headptr->prev=newnode; headptr=newnode;} newnode->prev = tailptr; tailptr->next = newnode; tailptr
Inserting in Front: Reminders prev of head should properly point to tail head should point to the new node
Insert a Node in the End void InsertTail(nodeptr newnode) { if (tailptr==NULL) headptr=tailptr; headptr=tailptr; headptr data newnode tailptr tailptr->next =newnode; newnode->prev=tailptr; tailptr=newnode;headptr->prev=tailptr}
Insert Node at End:Reminders next of tail should point to head prev of head should point to tail
Bonus Question: What if you do not have a tail pointer? How should you keep track the end of the list?
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
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.
Deleting a node cursor Modify SearchNode 1. Declare a nodeptr prev; 2. Let prev follow cursor before it goes to next prev 8 null null
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 cursor prev 8 null null Deleting a node
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 cursor prev 87
Applications of Linked List database records preliminary for advanced data structures such as binary trees, heaps,etc computing power is based on linked list
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?
We are done with linked lists Next 2 weeks- Stacks and Queues Applicatio of stacks and queus
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%