Linked Lists - Advanced Programming - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Advanced Programming which includes Class Data Members, Class Data Initializers, Class Objects, Class Instructor, Constructor Initializer, User Defined Types, Appropriate Constructors, Class Declaration, Name of Base Class etc. Key important points are: Linked Lists, Elementary Data Structures, List Implementation, Self-Referential Class Objects, Nodes, Pointer Links, Number of Data Elements, Self-Referential Structures, Null Pointer

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharmaraaj
dharmaraaj 🇮🇳

4.4

(68)

145 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Elementary Data Structures
Linked Lists
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Linked Lists - Advanced Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Elementary Data Structures Linked Lists

List Implementation using Linked Lists

  • Linked list
    • Linear collection of self-referential class objects,called nodes
    • – Connected by pointer linksAccessed via a pointer to the first node of the list
    • Link pointer in the last node is set to null to mark thelist’s end
  • Use a linked list instead of an array when
    • You have an unpredictable number of data elements
    • You want to insert and delete quickly.

Linked Lists

• Types of linked lists: – Singly linked list

  • • Begins with a pointer to the first nodeTerminates with a null pointer

– Circular, singly linked^ •^ Only traversed in one direction

  • Pointer in the last node points back to the first node

– Doubly linked list • Two “start pointers” – first element and last element

  • • Each node has a forward pointer and a backward pointerAllows traversals both forwards and backwards

– Circular, doubly linked list • Forward pointer of the last node points to the first node andbackward pointer of the first node points to the last node

• In a linked representation, data isnot stored in a contiguous manner.Linked Representation of Data

Instead, data is stored at randomlocations and the current datalocation provides the information regarding the location of the nextdata.

Adding item Q: What is the cost of adding an item? Q: how about adding 498 on to the linked list 300 and 800

onto the linked list

Deleting item Q: What is the cost of deleting an item? Q: What is the cost of searching for an 358 from the linked list

item?

Conventions of Linked List

There are several conventions for the link toindicate the end of the list.

1. a null link that points to no node (0 or NULL)

2. a dummy node that contains no item

3. a reference back to the first node, making it a circular list.

bat  cat  sat  vat NULL

*Figure 4.1: Usual way to draw a linked list (p.139)

Singly Linked List

Two Node Linked List

10  20 NULL

ptr

list_pointer create2( ){

/* create a linked list with two nodes */list_pointer first, second;

first = (list_pointer) malloc(sizeof(list_node));second = ( list_pointer) malloc(sizeof(list_node));

second -> link = NULL;second -> data = 20;

first -> data = 10;first ->link = second;

} return first;

Linked List Manipulation

• – List Traversal Let START be a pointer to a linked list in memory. Write an algorithm toAlgorithms

  • print the contents of each node of the listAlgorithm
      1. set PTR = STARTrepeat step 3 and 4 while PTR ≠ NULL
      1. print PTR->DATAset PTR = PTR -> LINK
    1. stop

START^10002000 3000 PTR PTR = LINK[PTR]

(^10 10) Data^ Link 20 20 30 30 40 40 50

Insert an Item

• – Insertion into a Listed List Let START be a pointer to a linked list in memory with successive

nodes A and B. Write an algorithm to insert node N between nodesA and B.

  • Algorithm 1. 2. Set PTR = STARTRepeat step 3 while PTR ≠ NULL

3. 4. 5. If PTR == A, thenSet N->LINK = PTR -> LINK (or = B)Set PTR->LINK = N

6. 7. 8. else exitSet PTR=PTR->LINK

9. 10. If PTR == NULL insertion unsuccessfulStop

START 1000 2000 Node A 3000 Node B 4000 5000

PTR

START 1000 2000 Node A 3000 Node B 4000 5000 3 cases: first node, last node, in-between node. ( ex: if ITEM = 500? if ITEM = 6000?)^ Node N^3500

• – Deletion from a Linked List Let START be a pointer to a linked list in memory that contains integer data. WriteDelete an Item

  • an algorithm to delete note which contains ITEM.Algorithm
      1. Set PTR=START and TEMP = STARTRepeat step 3 while PTR ≠ NULL
        1. If PTR->DATA == ITEM, thenelse Set TEMP->LINK = PTR -> LINK, exit
      1. TEMP = PTRPTR = PTR -> LINK
    1. Stop

3 cases: first node, last node, in-between node. ( ex: if ITEM = 1000? if ITEM = 5000?)^ PTR

START 1000 2000 Node A 3000 Node N 3500 Node B 4000 5000 START 1000 2000 Node A 3000 Node N 3500 Node B 4000 5000

ITEM^ …..^3500 TEMP