CS 241 Data Organization Linked Lists March 27, 2018 Linked List - - PowerPoint PPT Presentation

cs 241 data organization linked lists
SMART_READER_LITE
LIVE PREVIEW

CS 241 Data Organization Linked Lists March 27, 2018 Linked List - - PowerPoint PPT Presentation

CS 241 Data Organization Linked Lists March 27, 2018 Linked List A B C D E NULL A linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (a


slide-1
SLIDE 1

CS 241 Data Organization Linked Lists

March 27, 2018

slide-2
SLIDE 2

Linked List

A B C D E NULL

  • A linked list is a data structure that consists of

a sequence of data records such that in each record there is a field that contains a reference (a link) to the next record in the sequence.

  • In the C programming language, the “link” is

usually implemented as a pointer. The link could, however, be implemented in other ways (i.e. as an array index).

slide-3
SLIDE 3

Why Linked Lists?

Why not just stick with arrays?

  • The order of the linked items may be different

from the order that the data items are stored in memory or on disk.

  • Size is not predetermined, so can make better

use of memory.

  • We can insert and remove elements without

having to reorganize the entire structure.

slide-4
SLIDE 4

Linked Lists in File Systems

  • Most file systems store data as linked lists of

data blocks.

  • “Defragmenting” a hard disk moves the blocks

to maximize the number of blocks that are physically adjacent.

slide-5
SLIDE 5

Linked List Access and Insertion

  • Access to the ith element requires walking the

list from the beginning and counting links to i. Such a process is said to have a time complexity of O(n).

  • Insertion or Deletion at a known access point

has a constant time complexity time O(1).

slide-6
SLIDE 6

Basic Structure

Nodes are self-referential structures.

struct ListNode { int data; struct ListNode* next; };

slide-7
SLIDE 7

Initializing

struct ListNode* createNode(int data) { struct ListNode* node = malloc(sizeof(struct ListNode )); node ->data = data; node ->next = NULL; return node; }

slide-8
SLIDE 8

Looking through a list

void printlist(struct ListNode* head) { struct ListNode* current = head; while (current != NULL) { printf("%d ", current ->data ); current = current ->next; } printf("\n"); }

slide-9
SLIDE 9

List Length

int listlength(struct ListNode* head) { struct ListNode* current = head; int count = 0; while (current != NULL) { count ++; current = current ->next; } return count; }

slide-10
SLIDE 10

Inserting an element

  • Beginning of the list
  • Middle of the list
  • End of the list
slide-11
SLIDE 11

Insert at beginning

struct ListNode* newNode = createNode(data ); newNode ->next = head; head = newNode;

slide-12
SLIDE 12

Insert in middle

struct ListNode* newNode = createNode(data ); newNode ->next = currentNode ->next; currentNode ->next = newNode;

slide-13
SLIDE 13

Remove from beginning

struct ListNode* node = head; head = node ->next; free(node );