CS 192 Lecture 19 Winter 2003 February 9-10, 2004 Dr. Shafay Shamail.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being.
Linked Lists A linked list is a series of connected nodes Each node contains at least –A piece of data (any type) –Pointer to the next node in the list.
Data Structures Data Structures Topic #5. Today’s Agenda Other types of linked lists –discuss algorithms to manage circular and doubly linked lists –should.
Saturday, February 3, 2007 “Programmer - an organism that turns coffee into software.” - Anonymous.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
ENEE150 – 0102 ANDREW GOFFIN Linked List/Project 3.
Algorithms and Data Structures
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
UNIT – I Linked Lists.
Doubly Linked List Review - We are writing this code
Traversing a Linked List
Linked lists.
Linked Lists.
Edvin von Otter & Todd Barker
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Building Java Programs
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
Chapter 16 Linked Structures
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Linked Lists.
CS148 Introduction to Programming II
slides created by Marty Stepp
slides adapted from Marty Stepp
Linked lists.
Linked Lists.
Linked Lists.
slides created by Marty Stepp
Presentation transcript:

CS 192 Lecture 19 Winter 2003 February 9-10, 2004 Dr. Shafay Shamail

Linked Lists typedef struct Node { int data; Node* next; } Node;

/*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...

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; }

Length of the linked list... int length(Node* head) { Node* current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; }

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.

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;

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;

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); }

Building the linked list... The problem is to write a general function which adds a single node to head end of any list.

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; }

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; }

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; }

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

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; }

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?

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; }

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?