Title: STRUCTURES AND LIST PROCESSING
1STRUCTURES AND LIST PROCESSING
- CHAPTER 10
- (PART 1)
- prepared by Senem Kumova Metin
- modified by Ilker Korkmaz
2Content
- Chapter 10 of the textbook will be covered in a
few sequential parts of lectures in class. - The content of this presentation, Part1,
includes self-referential structures, linear
linked lists, storage allocation, and list
operations.
3Self Referential Structures (I)
- struct node
- int data
- struct node next
- struct node a,b,c
- a b c
- a.data1 b.data2 c.data3
- a.next b.next c.nextNULL / NULL means 0 /
- a b c
A structure of type struct node
data
next
data
next
data
next
data
next
2
NULL
3
NULL
1
NULL
4Self Referential Structures (II)
- a.next b
- b.next c
- a b c
- (a.next).data ? a.next-gtdata ?
- (b.next).data ? b.next-gtdata ?
2
c
3
NULL
1
b
5LINEAR LINKED LISTS (I)
data
next
data
next
data
next
NULL
head-gtnext
head
typedef struct linked_list int data
struct linked_list next ELEMENT
6LINEAR LINKED LISTS (II)
typedef struct linked_list int data
struct linked_list next ELEMENT typedef
ELEMENT LINK main() LINK head // ELEMENT
head / Create List / head
malloc(sizeof(ELEMENT))// head-gtdata1 head-
gtnextNULL / Add 1st element / head-gtnext
malloc(sizeof(ELEMENT))// head-gtnext-gtdata
2 head-gtnext-gtnextNULL
1
head
NULL
1
2
NULL
7LINEAR LINKED LISTS (III)
main() LINK head // ELEMENT head
ELEMENT n head malloc(sizeof(ELEMENT))
head-gtdata1 head-gtnextNULL / Add 1st
element / head-gtnext malloc(sizeof(ELEMENT))
head-gtnext-gtdata 2 head-gtnext-gtnextNULL
/ Add 2nd element / head-gtnext-gtnext
malloc(sizeof(ELEMENT)) head-gtnext-gtnext-gtdata
3 head-gtnext-gtnext-gtnextNULL
1
head
NULL
1
2
NULL
head
1
2
3
NULL
8LINEAR LINKED LISTS (IV)
head
1
3
NULL
2
? next ?
typedef struct linked_list int data
struct linked_list next ELEMENT typedef
ELEMENT LINK
main() LINK head ELEMENT n head
malloc(sizeof(ELEMENT)) head-gtdata1
head-gtnextNULL head-gtnextmalloc(sizeof(ELEMEN
T)) head-gtnext-gtdata 3 head-gtnext-gtnextNULL
/INSERT AT THE MIDDLE / n.data2
n.nexthead-gtnext head-gtnextn
9LINEAR LINKED LISTS (V)
head
1
3
NULL
2
? next ?
main() LINK head LINK n head
malloc(sizeof(ELEMENT)) head-gtdata1
head-gtnextNULL head-gtnextmalloc(sizeof(ELEMEN
T)) head-gtnext-gtdata 3 head-gtnext-gtnextNULL
/ ALLOCATE THE MEMORY FOR THE NEW NODE /
n malloc(sizeof(ELEMENT)) /INSERT AT THE
MIDDLE / n-gtdata2 n-gtnexthead-gtnext
head-gtnextn
typedef struct linked_list int data
struct linked_list next ELEMENT typedef
ELEMENT LINK
10LINEAR LINKED LISTS AN ALTERNATIVE
IMPLEMENTATION (I)
- Use a node structure to represent a single
element of the list. - Use a list structure to show the first and the
last elements within the list. - Manage the list through its operations
node3
node2
node1
NULL
List Structure Head points node1 Tail points node3
11LINEAR LINKED LISTS AN ALTERNATIVE
IMPLEMENTATION (II)
- NODE DECLARATION
- struct node
- int data
- struct node next
-
- LIST DECLARATION
- struct list
- struct node head
- struct node tail
- / one more member to represent the number of
the elements within the list may be used/ -
- AN EXAMPLE DECLARATION FOR AN INSERT OPERATION
- void append(struct list myListPtr, int
myData) - / append inserts a new node, which includes
myData, to the tail of myListPtr / - // OR ? void append(struct list myList,
int myData)
12Next lecture (Part2)
- We will cover the list processing functions, such
as creation, insertion, deletion, counting,
printing,...
13Linear Linked List Operations
- Creating an element,
- Inserting an element into a list,
- Searching for a key element in a list,
- Sorting the elements of a list,
- Deleting an element from a list,
- Deleting all elements of a list,
- Displaying all elements of a list,
- ...
14Linked List Types
- Single linked lists (providing the next)
tail
head
Double linked lists ( providing the next and the
previous)
head
NULL
NULL
tail
Circular linked lists (providing the next)
next
15Linear Linked Lists EXAMPLE a character
listNOTE The following slides illustrate the
list implementation, which is similar to the one
used in the textbook. However, the alternative
method based on the slides 9 and 10 may also be
implemented.TO DO implement the operations of
the alternative list approach on the 10th slide.
16List declaration
include ltassert.hgt include ltstdio.hgt include
ltstdlib.hgt define MAXLINE 100 typedef
char DATA / will use char in examples
/ struct linked_list DATA d struct
linked_list next typedef struct
linked_list ELEMENT typedef ELEMENT LINK
17Creating the list iteratively
include "list.h" LINK s_to_l(char s) LINK
head NULL, tail int i if (s0 !
'\0') / first element / head
malloc(sizeof(ELEMENT)) head -gt d
s0 tail head for (i 1 si !
'\0' i) / add to tail / tail
-gt next malloc(sizeof(ELEMENT)) tail
tail -gt next tail -gt d si
tail -gt next NULL / end of list /
return head
18Creating the list recursively
include "list.h LINK string_to_list(char s)
LINK head if (s0 '\0') / base
case / return NULL else head
malloc(sizeof(ELEMENT)) head -gt d
s0 head -gt next string_to_list(s
1) return head
19Counting the elements of the list (iterative
version)
include "list.h" int count_list(LINK head)
int count for ( head ! NULL head head
-gt next) count return count
20Counting the elements of the list (recursive
version)
include "list.h int count_list(LINK head)
if (head NULL) return 0 else return(1
count_list(head -gt next))
21Display the elements of the list (iterative
version)
include "list.h" void wrt_list(LINK head)
LINK p if (head NULL) printf(NULL
list) else for (p head p ! NULL p
p -gt next) putchar(p -gt d)
22Display the elements of the list (recursive
version)
include "list.h" void wrt_list(LINK head) if
(head NULL) printf(NULL list) else
printf(c ?, head -gt d)
wrt_list(head -gtnext)
23Inserting a new node into a point within the list
include "list.h" void insert(LINK p1, LINK p2,
LINK q) assert (p1-gt next p2) / if the
expression inside assert is false, the system
will print a message and the program will be
aborted/ p1-gtnext q q-gtnext p2
initially
p2
p1
q
24Deleting all elements of a list(iterative
version)
include "list.h" void delete(LINK head)
LINK p if (head NULL) printf(NULL
list) else for (p head p ! NULL p
p -gt next) free(p)
25Deleting all elements of a list(recursive
version)
include "list.h" void delete_list(LINK
head) if (head ! NULL)
delete_list(head -gtnext) free(head)