Lecture 3 - Linked Lists I.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

ThumbsUpBinomial

Uploaded by ThumbsUpBinomial

Nile University

Tags

linked lists data structures C++ programming

Full Transcript

CSCI 207: Fundamentals of Data Structures & Algorithms Lecture 3 Linked Lists I 1 Review Arrays An array stores a sequence of values arrayType arrayName[ numberOfElements ]; Drawback: –...

CSCI 207: Fundamentals of Data Structures & Algorithms Lecture 3 Linked Lists I 1 Review Arrays An array stores a sequence of values arrayType arrayName[ numberOfElements ]; Drawback: – capacity of array fixed (it doesn’t change) – must know max number of values at compile time – either the program runs out of space or wastes space – Arrays hold data of the same data type Recall that a static data structure has a fixed size Dynamic Data Structures Dynamic data structures hold an unknown number of elements. (capacity can grow and shrink as program runs) Generic type data structures hold data elements of different data types. Types of Dynamic & Generic Data Structures Linked Lists Stacks Queues Trees Linked Lists A B C Head 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 Head: pointer to the first node The last node points to NULL node A data pointer More Terminology A node’s successor is the next node in the sequence ▪ The last node has no successor A node’s predecessor is the previous node in the sequence ▪ The first node has no predecessor A list’s length is the number of elements/nodes in it ▪ A list may be empty (contain no elements/nodes) 6 Singly-Linked Lists Here is a singly-linked list (SLL): myList a b c d Each node contains a value and a link to its successor (the last node has no successor) The head points to the first node in the list (or contains the null link if the list is empty) 7 SLL - Simple Example class Node { private: int value; Node* next; public: value next Node () { next = NULL; } // constructor int getValue () { return value; } void setValue (int v) { value = v; } void setNext (Node* n) { next = n; } Node* getNext () { return next; } }; 8 SLL - Simple Example one two value next value next three four value next value next one two 44 97 value next value next three four 23 17 value next value next 9 SLL - Simple Example head one 44 value next two 97 value next three 23 value next four 17 value next 10 Creating SLL Links in C++ myList 44 97 23 17 class Node { int value; Node* next; Node (int v, Node* n) { // constructor value = v; next = n; } } Node *temp=new Node(17, NULL); temp = new Node(23, temp); temp = new Node(97, temp); Node *myList = new Node(44, temp); Creating SLL Links in C++ Node *myList; mylist = new Node(44, new node(97, new node(23, new node(17, NULL) ))); myList 44 97 23 17 Creating links in C++ To create the list with ("one", "two", "three"): Node *numerals; numerals = new Node("one", new Node("two", new Node("three", null))); numerals one two three Traversing a SLL The following method traverses a list (and prints its elements): void print() { // method in Node class Node *here = this; while (here != NULL) { cout

Use Quizgecko on...
Browser
Browser