Download presentation
Presentation is loading. Please wait.
1
CS 192 Lecture 19 Winter 2003 February 9-10, 2004 Dr. Shafay Shamail
2
Linked Lists typedef struct Node { int data; Node* next; } Node;
3
/*Build the list {1, 2, 3}*/ Node* buildOneTwoThree() { Node* head = NULL; Node* second = NULL; Node* third = NULL; head = new Node; second = new Node; third = new Node; head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = NULL; return head; } Linked Lists - easy example first...
4
Display the linked list... void display(Node * mylist) { while (mylist!=NULL) { cout data<<endl; mylist=mylist->next; } int main(void) { Node * list=buildOneTwoThree(); display(list); return 0; }
5
Length of the linked list... int length(Node* head) { Node* current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; }
6
Building the linked list... buildOneTwoThree() is fine as an example of pointer manipulation code, but it's not a general mechanism to build lists. The best solution will be an independent function which adds a single new node to any list. We can then call that function as many times as we want to build up any list.
7
Building the linked list... The classic 3-Step Link In operation which adds a single node to the front of a linked list. The 3 steps are… 1) Allocate Allocate the new node in the heap and set its.data to whatever needs to be stored. Node* myNode; myNode = new Node; myNode->data = data_client_wants_stored;
8
Building the linked list... 2) Link Next Set the.next pointer of the new node to point to the current first node of the list. (This is actually just a pointer assignment remember: "assigning one pointer to another makes them point to the same thing.”) myNode->next = head; 3) Link Head Change the head pointer to point to the new node, so it is now the first node in the list. head = myNode;
9
Building the linked list... void linkTest() { Node* head = buildOneTwoThree(); Node* myNode; myNode = new Node; myNode->data = 4; myNode->next = head; head = myNode; // now head points to the list _________ ? display(head); }
10
Building the linked list... The problem is to write a general function which adds a single node to head end of any list.
11
Building the linked list... void wrongInsertAtFront(Node* head, int data) { Node* myNode = new Node; myNode->data = data; myNode->next = head; head = myNode; // NO this line does not work! } int main(void) { Node* head = buildOneTwoThree(); wrongInsertAtFront(head, 4); wrongInsertAtFront(head, 5); display(head); return 0; }
12
Building the linked list... void correctInsertAtFront(Node* &head, int data) { Node* myNode = new Node; myNode->data = data; myNode->next = head; head = myNode; } int main(void) { Node* head = buildOneTwoThree(); correctInsertAtFront(head, 4); correctInsertAtFront(head, 5); display(head);//OUTPUT? return 0; }
13
Building the linked list... void correctInsertAtFront(Node* &head, int data) { Node* myNode = new Node; myNode->data = data; myNode->next = head; head = myNode; } int main(void) { Node *head=NULL; correctInsertAtFront(head, 4); correctInsertAtFront(head, 5); display(head); //OUTPUT? return 0; }
14
Deleting from the linked list... void deleteFromList(Node*& headref, int num) { if (headref == NULL) return; else if (headref->data ==num) { Node* temp=headref; headref=headref->next; delete temp; } //continued on next page
15
Deleting from the linked list... else { Node* current=headref; while((current->next !=NULL) && (current->next->data !=num)) { current=current->next; } if (current->next == NULL) return; //not in list Node* temp=current->next; current->next = current->next->next; delete temp; }
16
Deleting from the linked list... int main(void) { Node* head = buildOneTwoThree(); display(head); deleteFromList(head, 1); deleteFromList(head, 2); cout<<endl; display(head); return 0; } //OUTPUT?
17
Appending at end of the linked list... void appendNode(Node*& headRef, int num) { Node* current = headRef; Node* myNode; myNode = new Node; myNode->data = num; myNode->next = NULL; if (current == NULL) {// special case for length 0 headRef = myNode; } else { while (current->next != NULL) {// Locate the last node current = current->next; } current->next = myNode; }
18
Appending at end of the linked list... int main(void) { node *head=NULL; appendNode(head, 15); appendNode(head, 16); appendNode(head, 17); appendNode(head, 18); display(head); return 0; } //Output?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.