Title: STRUCTURES AND LIST PROCESSING
1STRUCTURES AND LIST PROCESSING
2Self Referential Structures
- struct list
- int data
- struct list next
- struct list a,b,c
- a b c
- a.data1 b.data2 c.data3
- a.next b.next c.nextNULL
- a b c
A structure of type struct list
data
next
data
next
data
next
data
next
2
NULL
3
NULL
1
NULL
3Self Referential Structures
- 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
4LINEAR LINKED LISTS
data
next
data
next
data
next
NULL
head-gtnext
head
typedef struct linked_list int data
struct linked_list next ELEMENT
5LINEAR LINKED LISTS
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-gtne
xtNULL / Add 1st element / head-gtnext
malloc(sizeof(ELEMENT)) head-gtnext-gtdata
2 head-gtnext-gtnextNULL
1
NULL
head
1
2
NULL
head
6LINEAR LINKED LISTS
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
NULL
head
1
2
NULL
head
1
2
3
NULL
head
7LINEAR LINKED LISTS
1
3
NULL
head
2
NULL
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(ELEMENT
)) head-gtnext-gtdata 3 head-gtnext-gtnextNULL
n.data2 n.nexthead-gtnext head-gtnextn
8LINEAR LINKED LISTS
1
3
NULL
head
2
NULL
main() LINK head LINK n head
malloc(sizeof(ELEMENT)) head-gtdata1
head-gtnextNULL head-gtnextmalloc(sizeof(ELEMENT
)) head-gtnext-gtdata 3 head-gtnext-gtnextNULL
n malloc(sizeof(ELEMENT)) n-gtdata2
n-gtnexthead-gtnext head-gtnextn
typedef struct linked_list int data
struct linked_list next ELEMENT typedef
ELEMENT LINK
9Linear Linked List Operations
- Creating a list element,
- Prepending elements on top of lists,
- Inserting elements into lists,
- Appending to end of lists,
- Finding/searching elements in the list
- Sorting elements,
- Deleting,
- Moving elements around in the list.
10Linked List Types
- Single linked lists (next)
next
Double linked lists (next previous)
NULL
Circle linked lists (next)
next
11Linear Linked Lists EXAMPLE
12Creating a List 1st Declare the List
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
13Creating a List Using Iteration
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
14Creating a List Using Recursion
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
15Count Elements of a List Using Iteration
include "list.h" int count_list(LINK head)
int count for ( head ! NULL head head
-gt next) count return count
16Count Elements of a List Using Recursion
include "list.h int count_list(LINK head)
if (head NULL) return 0 else return(1
count_list(head -gt next))
17Print Elements in a List Using Iteration
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)
18Print Elements in a List Using Recursion
include "list.h" void wrt_list(LINK head) if
(head NULL) printf(NULL list) else
printf(c ?, head -gt d)
wrt_list(head -gtnext)
19Insertion of Elements in a 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
20Delete Elements in a List Using Iteration
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)
21Delete Elements in a List Recursively
include "list.h" void delete_list(LINK
head) if (head ! NULL)
delete_list(head -gtnext) free(head)
22A LINKED LIST IMPLEMENTATION OF A QUEUE
queue
Queue First-In-First-Out (FIFO) data structure
cnt
front
rear
elem
elem
elem
23The header file queue.h
/ queue.h/ define EMPTY 0 define
FULL 10000 typedef unsigned int
data typedef enum false, true
boolean struct elem / an element in
the queue / data d struct elem
next typedef struct elem elem
struct queue int cnt / a count
of the elements / elem front / ptr
to the front element / elem rear
/ ptr to the rear element / typedef
struct queue queue
24The header file queue.h (cont d)
void initialize(queue q) void
enqueue(data x, queue q) data
dequeue(queue q) data front(const queue
q) boolean empty(const queue q) boolean
full(const queue q)
25Basic queue routines
- include "queue.h"
- void initialize(queue q)
- q -gt cnt 0
- q -gt front NULL
- q -gt rear NULL
- data dequeue(queue q)
- data x
- // unsigned int x
- elem p
- x q -gt front -gt d
- p q -gt front
- q -gt front q -gt front -gt next
- q -gt cnt--
- free(p)
- return x
void enqueue(data x, queue q) elem p
p malloc(sizeof(elem)) p -gt d x p
-gt next NULL if (!empty(q)) q -gt
rear -gt next p q -gt rear p
else q -gt front q -gt rear p q
-gt cnt
26Basic queue routines (cont d)
- data front(const queue q)
-
- return (q -gt front -gt d)
-
- boolean empty(const queue q)
-
- return ((boolean) (q -gt cnt EMPTY))
-
- boolean full(const queue q)
-
- return ((boolean) (q -gt cnt FULL))
-
27- /Using queues to schedule two resources./
- include "queue.h"
- int main(void)
-
- int c, cnt_a 0, cnt_b 0
- data pid / process id number /
- queue a, b
- initialize(a) initialize(b)
- / Enqueue the requests. /
- while ((c getchar()) ! EOF)
- switch (c)
- case 'A'
- assert(scanf("u", pid) 1)
- if (!full(a)) enqueue(pid, a)
- break
- /Dequeue the requests and print them./
- printf("A's schedule\n")
- while (!empty(a))
-
- pid dequeue(a)
- printf("JOB u is d\n,cnt_a, pid)
-
- printf("B's schedule\n")
-
- while (!empty(b))
-
- pid dequeue(b)
- printf("JOB u is d\n", cnt_b, pid)
-
- putchar('\n')
- return 0