SOFTWARE DEVELOPMENT IN C CM9041 - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

SOFTWARE DEVELOPMENT IN C CM9041

Description:

Consider the following linked list, where to maintain ascending order, we only ... from a linked list. To delete a data item from a linked list involves (assuming it ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 8
Provided by: rho2
Category:

less

Transcript and Presenter's Notes

Title: SOFTWARE DEVELOPMENT IN C CM9041


1
SOFTWARE DEVELOPMENT IN CCM904-1
  • Linked Lists and Insertion
  • Lecture 12

2
Insertion into a linked list
  • To insert a data item into a linked list
    involves
  • creating a new node containing the data,
  • finding the correct place in the list, and
  • linking in the new node at this place.

3
12
23
34 -
15
3
  • Consider the following linked list, where to
    maintain ascending order, we only know where to
    link in the new node (containing 15) when ptr
    finds the 23 (23 gt 15), and we need the previous
    node as well to make the link to new
  • (this is true even if previous is the last node!)

prev
ptr
First
3
12
23
34 -
15
new
  • We also need to check whether the new node should
    be inserted before the first node.

4
  • Link insert(int data, Link ptr)
  • / returns pointer to a list (ie type Link)
    with a node containing 'data' item inserted into
    correct position of ascending sequence /
  • Link new, prev, first
  • if (ptr NULL data lt ptr -gt value)
  • // insert as new first node
  • new (Link)malloc(sizeof(struct node))
  • new -gt value data
  • new -gt next ptr
  • return (new) // new node is first
  • else // not first one
  • first ptr // remember start
  • prev ptr
  • ptr ptr -gt next
  • while (ptr ! NULL data gt ptr -gt value)
  • prev ptr
  • ptr ptr -gt next
  • // link in new node between prev node and ptr
    node

5
Deletion of a data item from a linked list.
  • To delete a data item from a linked list involves
    (assuming it occurs only once!)
  • finding the data item in the list, and
  • linking out this node, and
  • freeing this node as free space.

6
  • Consider the following list, when ptr finds the
    23, we need the previous node to make the link to
    the next one after ptr (i.e. ptr -gt next).
  • (this is true even if ptr is the last node!)
  • We also need to check whether the first node is
    to be deleted.

ptr
prev
First
3
12
23
34 -
7
  • include "lll.h"
  • // delete the data item from an ascending list
  • Link delete_item(int data, Link ptr)
  • Link prev, first
  • first ptr // remember start
  • if (ptr NULL)
  • return NULL
  • else
  • if (data ptr -gt value)
  • ptr ptr -gt next // second node
  • free(first) // free up deleted node return
    ptr // second is now start
  • else // check rest of list
  • prev ptr
  • ptr ptr -gt next
  • while (ptr ! NULL data gt ptr -gt value)
  • prev ptr
  • ptr ptr -gt next
Write a Comment
User Comments (0)
About PowerShow.com