linked lists
Download
Skip this Video
Download Presentation
Linked Lists

Loading in 2 Seconds...

play fullscreen
1 / 15

Linked Lists - PowerPoint PPT Presentation


  • 107 Views
  • Uploaded on

Linked Lists. CSE 2451 Rong Shi. Dynamic memory reminder. Allocate memory during run-time malloc () and calloc () – return a void pointer to memory or a NULL pointer if allocation failed The programmer must check for the NULL value before dereferencing the pointer

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Linked Lists' - barth


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
linked lists

Linked Lists

CSE 2451

Rong Shi

dynamic memory reminder
Dynamic memory reminder
  • Allocate memory during run-time
  • malloc() and calloc() – return a void pointer to memory or a NULL pointer if allocation failed
    • The programmer must check for the NULL value before dereferencing the pointer
  • free() – relinquishes memory allocated during run-time
    • Every malloc and calloc call should have a corresponding free call later
    • Pointer value is not changed after free, but it is not safe to dereference it (dangling pointer)
  • Memory leak
    • Memory that has been
      • Dynamically allocated,
      • Not freed, and
      • Is no longer in use/there is no pointer to it
dynamic memory example practice
Dynamic memory example/practice

int x;

int*pi;

pi = (int*)malloc(5 * sizeof(int));

if (pi == NULL)

{

printf(“Out of memory!\n”);

exit(1);

}

for (x = 0; x < 5; x +=1)

*pi++ = 1;

// Print array

// free memory

  • What are the values in the new memory before initializing to zero?
  • Where is pi pointing to after the for loop?
  • How to free?
dynamic arrays realloc
Dynamic arrays – realloc

See remember.c example

linked list
Linked list
  • Data structure consisting of a group of nodes which together represent a sequence
  • A node consists of
    • Data
    • A reference/link/pointer to the next node in the sequence
  • Advantages
    • Efficient insertion or removal of elements from any position
    • Data items do not have to be stored contiguously in memory
  • Disadvantage
    • No random access or efficient indexing
singly linked list node
Singly linked list node

/* Node Structure */

struct node {

intdata;

structnode *next;

}

doubly linked list node
Doubly linked list node

/* Node Structure */

struct node {

intdata;

structnode *next;

struct node *prev;

}

null terminated lists
NULL terminated lists
  • Singly linked
  • Doubly linked

Boxes with x represent NULL values

circularly linked lists
Circularly linked lists
  • Last pointer is set to the first node
accessing lists
Accessing lists
  • Still need a pointer to that refers to the first node in the list: head pointer

struct node * head

adding nodes
Adding nodes
  • Step through the list until the correct location is found (for a sorted list)
  • newNode’s pointer = node’s pointer
  • node’s pointer = newNode
list search
List search

found = false;

ptr = head;

while(ptr != NULL)

{

if(ptr->data = = val) {

found = true;

break;

}

else {

ptr= ptr->next;

}

}

// found = true then ptr points to that node

// found = false then value is not in the list, ptr is NULL

removing nodes
Removing nodes
  • Step through the list until the node is found
  • node’s pointer = node.next’s pointer
  • Need a pointer to node.next for free
additional examples
Additional examples
  • http://rosettacode.org/wiki/Linked_list
  • http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html
    • Building a linked list, but not freeing it
ad