Introduction to C Programming CE003121 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Introduction to C Programming CE003121

Description:

This is very cumbersome for a large array and if many insertions are required. ... But, if we make the array very large, then most of the time most of the space is ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 12
Provided by: rgh1
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming CE003121


1
Introduction to C ProgrammingCE00312-1
  • Lecture 19
  • Linear Linked Lists

2
Insertion into an ordered array
3 5 8 12 23 34 44 50 56 76

A
0 1 2 3 4 5 6 7 8
9 10 11
  • Suppose we wanted to insert the value, 7.
  • To keep the integers in ascending order, we cant
    just store this 7 in element A10.
  • We would have to shift all elements from A2 to
    A9 one location to the right, and then store
    the 7 in A2.

3
Insertion into an ordered array
  • This is very cumbersome for a large array and if
    many insertions are required.
  • Sooner or later we would run out of space in the
    array
  • But, if we make the array very large, then most
    of the time most of the space is wasted!
  • Deletion of values from the array causes similar
    problems.
  • So as not to leave gaps, many values would have
    to be shifted left for each deletion.
  • We should not use arrays if we need to make
    insertions and deletions.

4
Linear Linked List
list
3
5
8
12 -
  • Each node may be stored anywhere in memory
  • and can only be reached by a pointer.
  • The pointer variable, list, points to the first
    node
  • and each node points to the next node.
  • The last node has a NULL pointer (-).

5
Insertion into a ordered Linear Linked List
list
3
5
8
12 -
7
new
  • To insert an item, e.g. 7, between two nodes
    requires
  • creating a new node and
  • linking it with the other two nodes using just
    two pointers.

6
Node Structure
value next
  • struct node
  • int value
  • struct node next
  • typedef struct node Listpointer
  • // Listpointer is new type

7
Here is a header file - LLL.h containing
structure type definitions and function
prototypes for linked lists include
"stdio.h // NULL defined here include
"stdlib.h" // for malloc function struct node
int value struct node next typedef
struct node Listpointer // new type void
print_list(Listpointer) // prototypes void
print_reverse(Listpointer) Listpointer
insert(int, Listpointer) int add_items(Listpoint
er) Listpointer delete(int, Listpointer) int
search(int, Listpointer)
8
print_list function
  • When ptr is NULL then it is not pointing to
    anything,
  • ie there is no list, or in the loop, no more
    nodes in the list.
  • include "LLL.h // structure,type,prototypes
  • void print_list(Listpointer ptr)
  • while (ptr ! NULL)
  • printf("d\n", ptr -gt value)//print value
  • ptr ptr -gt next // point to next

9
ptr
3
5
8
12 -
value next
10
add_items function
  • include "LLL.h
  • int add_items(Listpointer ptr)
  • int sum 0
  • while (ptr ! NULL)
  • sum sum ptr -gt value
  • ptr ptr -gt next
  • return sum

11
include "LLL.h" int search(int item,
Listpointer ptr) while (ptr ! NULL) //
not end of list if (ptr -gt value
item) return 1 // true else
ptr ptr -gt next // end while return
0 // false // end search
Write a Comment
User Comments (0)
About PowerShow.com