COP3502_5_LinkedLists - Linked Lists Computer Science...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Linked Lists COP 3502 – Computer Science I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Linked Lists page 2 Linked Lists What are they? Abstraction of a list: i.e. a sequence of nodes in which each node is linked to the node following it. Why not just use an array? Each node in an array is stored, physically, in contiguous spaces in memory Arrays are fixed size (not dynamic) Inserting and deleting elements is difficult In an array of size 100, if we insert an element after the 10 th element, all the remaining 90 elements must be shifted.
Background image of page 2
Linked Lists page 3 Linked Lists Why use linked lists? They are dynamic; length can increase or decrease as necessary Each node does not necessarily follow the previous one in memory Insertion and deletion is cheap! Only need to change a few nodes (at most) Is there a negative aspect of linked lists? Getting to a particular node may take a large number of operations, as we do not know the address of any individual node
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Linked Lists page 4 Linked Lists In detail: A linked list is an ordered collection of data Each element (generally called nodes) contains the location of the next element in the list Each node essentially has two parts: The data part If this was a list of student records, for example, the data here may consist of a name, PID, social security number, address, phone, email, etc. The link part This link is used to chain the nodes together. It simply contains a pointer variable that points to the next node in the linked list Variable is often called “next” data next
Background image of page 4
Linked Lists page 5 Linked Lists - Example struct ll_node a, b, c; a.data = 1; b.data = 2; c.data = 3; a.next = b.next = c.next = NULL; data next 1 NULL a data next 2 NULL b data next 3 NULL c struct ll_node { int data; struct ll_node *next; }; Previous struct declaration:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Linked Lists page 6 Linked Lists – Example (cont.) a.next = &b; b.next = &c; a.next -> data a.next -> next -> data b.next -> next -> data data next 1 a data next 3 NULL c data next 2 b has value 2 has value 3 !! error
Background image of page 6
Linked Lists page 7 Linked Lists In detail: You can think of each node as a record The first part of the record is all the necessary data The final part of the record is a field that stores a pointer to the next node in the list Head of the list Each node of the list is created dynamically and points to the next node So from the first node, we can get to the second, and so on But how do you reach the first node? You must have a pointer variable that simply points to the first node of the list Simply called whatever you choose to name your list (myList)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
page 8 Linked Lists Example of a Linked List Don’t get confused over the “data” here It could be simply an integer value It could be 20+ separate fields of information storing name, address, phone, email, etc. data
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 48

COP3502_5_LinkedLists - Linked Lists Computer Science...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online