Title: CS201 Computer Sciences Faculty IEU
1CS201Computer Sciences FacultyIEU
Süleyman Kondakci suleyman_at_cryptocode.net
2Pointers
- Pointer variable containing address of another
variable - int x 5 / data variable /
- int y 7 / data variable /
- int ptr x / address operator /
-
5
7
1200
ptr
x
y
ptr 10
7
10
1200
y
ptr
x
3Pointers - 1
- Pointer variable containing address of another
variable - float f / data variable /
- float f_addr / pointer variable /
- f_addr f / address operator /
4Pointers - 2
f_addr 3.2 / indirection operator
/ float gf_addr /
indirectiong is now 3.2 / f 1.3
5Arrays and Pointers
An array name by itsels is an address or pointer
value! define N 100 int aN, i, p, q, sum
0 p a / is equivalent to p a0 / p
a 1 / is equivalent to p a1 / int ai
/ is equivalent to (a i) / Here (a i) is
the dereferencing of the expressin a i that
points i elements positions past in a p a /
points to the base of array a / q p 1 /
equivalent to q a1 / printf(d\n, q p)
printf("d\n", (int) q - (int) p) for (p a
p ltaN p) sum p for (i 0 i lt N i)
sum (a i)
6Pointer Arithmetic
int main(int argc, char argv) double a2,
p, q p a / points to the base of array a
/ q p 1 / equivalent to q a1
/ printf("\n") pri
ntf(" p d", p) printf("\n q d", q
) printf("\n q - p d", q - p) printf("\n q
- p d\n", (int) q - (int) p) printf(" a0
d", a0 ) printf("\n a1 d a1 )
7Pointer Example
include ltstdio.hgt void main(void) int
j int ptr ptrj / initialize ptr
before using it / / ptr4 does NOT
initialize ptr / ptr4 / j lt- 4
/ jptr / j lt- ??? /
8More pointers
int month12 / month is a pointer to base
address 430/ month3 7 / month address
3 int elements gt int at address (43034)
is now 7 / ptr month 2 / ptr points to
month2, gt ptr is now (4302 int
elements) 438 / ptr5 12 / ptr
address 5 int elements gt int at
address (43454) is now 12. Thus, month7 is
now 12 / ptr / ptr lt- 438 1
size of int 442 / (ptr 4)2 12 /
accessing ptr6 i.e., array9 /
- Now , month6, (month6), (month4)2,
ptr3, (ptr3) are all the same integer
variable.
9Pointers Summary
- int a 10, b 2, p
- p a // p is assigned address of a
- b p // b is assigned the value pointed by p
- b p is equivalent to b a
- An array name is an address or a pointer value
- Arrays and pointers can be subscripted
- int A10, p
- int b ai is equivalent to int b (a i)
- int c pi is equivalent to int c (p i)
- p a is equivalent to p a0
- p a 1 is equivalent to p a1
10Pointers Summary
All will sum the array ellements 1) for (p a
p ltaN p) sum p 2) for (i 0 i lt N
i) sum (a i) 3) p a for (i 0 i lt
N i) sum pi
11Call-by-value
Whenever variables are passed as arguments to a
function, their values are copied to the function
parameters void main() int a20 int
b30 swap (a, b) printf(d d , a,
b) void swap(int x, y) int
tmp tmpx xy ytmp
12Call-by-reference
Pointers are passed as arguments to a function,
their addresses are assigned to the function
parameters defined as pointers vioid
main() int a20 int b30 swap (a,
b) printf(d d , a, b) void swap(int
x, y) int tmp tmp x // get value
pointed by x. x y // assign value pointed
by y to x y tmp
13Structures
include ltstdio.hgt struct birthday int
month int day int year main()
struct birthday mybday mybday.day1
mybday.month1 mybday.year1977 printf(I was
born on d/d/d, birth.day,
birth.month, birth.year)
14More on Structures
struct person char name41 int age
float height struct / embedded or
nested structure / int month int
day int year birth struct
person me me.birth.year1977 struct
person class60 / array of info about
everyone in class / class0.nameCS206
class0.birth.year1971
15Accessing Members of Structures
struct exp char c int i float
arr10 struct exp exp1 struct exp
expPtr exp1.c A i 20 prinf(cd ,
exp1.c, exp1.i) expPtr-gtcB
expPtr-gti30 prinf(cd , expPtr-gtc,
expPtr-gti) expPtr exp1 prinf(cd ,
expPtr-gtc, expPtr-gti) prinf(cd ,
(expPtr).c, (expPtr).i)
16typdef Synonym for a data type
typedef int numbers numbers a_number / same
as int a_number / typedef struct person
Person Person me / same as struct
person me / typedef struct person
Personptr Personptr ptrtome / same as
struct person ptrtome/
- Easier to remember
- Clean code
17Linked Lists
- Nodes Represent data storage points
- Pointers are used to handle nodes
A node
int a char c char name kerem
NULL
18Linked 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.
19Linked List Types
next
Double linked lists
NULL
Circle linked lists
20Linked Lists
struct list int data struct list
next a
struct list a, b,c
a.data 1 b.data 2c.dat 3 a.nextb.nextc.
next NULL
a.next b b.next c
21Linear Linked Lists
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
22Linear Linked Lists
LINK head head malloc(sizeof(ELEMENT))
head-gtd A head-gtnext NULL
/ Lets add a second element / head -gtnext
malloc(sizeof(ELEMENT)) head-gtnext-gtd
B head-gtnext -gtnext NULL
/ Lets add a 3rd element / head -gtnext-gtnext
malloc(sizeof(ELEMENT)) head-gtnext-gtd -gtnext
C head-gtnext -gtnext -gtnext NULL
23Creating 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
24Creating 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
25Creating 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
26Count 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
27Count 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))
28Print Elements in a List Using Iteration
include "list.h" void wrt_list(LINK head)
LIST p if (head NULL) printf(NULL
list) else for (p head p ! NULL p p
-gt next) putchar(p -gt d)
29Print 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)
30Insertion of Elements in a List
include "list.h" Void insert(LINK p1, LINK p2,
LINK q) Assert (p1-gt next p2) p1-gtnext
q q-gtnext p2
initially
p2
p1
q
31Delete Elements in a List Using Iteration
include "list.h" void delete(LINK head)
LIST p if (head NULL) printf(NULL
list) else for (p head p ! NULL p p
-gt next) free(p)
32Delete Elements in a List Recursively
include "list.h" void elete_list(LINK
head) if (head ! NULL) delete_list(head
-gtnext) free(head)
33Queue Implementations
34Link-list based implementation
queue
Queue First-In-First-Out (FIFO) data structure
cnt
front
rear
elem
elem
elem
35The header file 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
36The header file queue.h (cont d)
void initialize(queue q) void
enqueue(data d, queue q) data
dequeue(queue q) data front(const queue
q) boolean empty(const queue q) boolean
full(const queue q)
37Basic queue routines (cont d)
include "queue.h" void initialize(queue q)
q -gt cnt 0 q -gt front NULL q -gt rear
NULL data dequeue(queue q) data d
elem p d q -gt front -gt d p q -gt
front q -gt front q -gt front -gt next q
-gt cnt-- free(p) return d
38Basic queue routines (cont d)
void enqueue(data d, queue q) elem p
p malloc(sizeof(elem)) p -gt d d 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
39Basic 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))
40- Array Based Queue Implementation
41Array Based Implementation
- Auxiliary queue operations
- front() returns the element at the front without
removing it - size() returns the number of elements stored
- isEmpty() returns a Boolean var indicating
whether the queue is empty - isFull() returns a Boolean var indicating
whether the queue is full - DisposeQueue(Queue Q) Deletes elements of queue Q
- The Queue ADT stores arbitrary objects
- Insertions and deletions follow the first-in
first-out scheme - Insertions are at the rear of the queue and
removals are at the front of the queue - Main queue operations
- Enqueue(Element e) inserts an element o at the
end of the queue - Dequeue() removes and returns the element at the
front of the queue
42Utility routine
include ltstdio.hgt include ltstdlib.hgt define
Error( Str ) FatalError( Str ) define
FatalError(Str) fprintf(stderr, "s\n", Str),
exit( 1 )
43Queue routines (queue.h)
struct QueueRecord int Capacity
int Front int Rear
int Size ElementType Array
QueueRecord typedef int ElementType typedef
struct QueueRecord ELEMENT typedef ELEMENT
Queue
44Queue routines (queue.h contd)
bool IsEmpty( Queue Q ) bool IsFull( Queue Q
) Queue CreateQueue( int MaxElements ) void
DisposeQueue( Queue Q ) void MakeEmpty( Queue Q
) void Enqueue( ElementType X, Queue Q
) ElementType Front( Queue Q ) void Dequeue(
Queue Q ) ElementType FrontAndDequeue( Queue Q )
45Queue routines (queue.c)
include "queue.h" include "error.h" ifndef
bool define bool define true 1 define false
0 endif define MinQueueSize ( 5 ) bool
IsEmpty( Queue Q ) return Q-gtSize
0 bool IsFull( Queue Q ) return (Q-gtSize
Q-gtCapacity)
46Queue routines (queue.c contd)
Queue CreateQueue( int MaxElements ) Queue
Q if( MaxElements lt MinQueueSize ) Error(
"Queue size is too small" ) Q malloc( sizeof(
ELEMENT) ) if( Q NULL ) FatalError( "Out
of space !!!" ) Q-gtArray malloc( sizeof(
ElementType ) ( MaxElements ) if(
Q-gtArray NULL ) FatalError( "Out of
space!!!" ) Q-gtCapacity MaxElements MakeEmpt
y( Q ) return Q
47Queue routines (queue.c contd)
void MakeEmpty( Queue Q ) Q-gtSize
0 Q-gtFront 1 Q-gtRear
0 void DisposeQueue( Queue Q ) if( Q !
NULL ) free( Q-gtArray )
free( Q ) static int Succ( int Value, Queue
Q ) if( Value Q-gtCapacity ) Value
0 return Value
48Queue routines (queue.c contd)
void Enqueue( ElementType X, Queue Q )
if( IsFull( Q ) ) Error( "Full queue" )
else Q-gtSize Q-gtRear Succ( Q-gtRear, Q
) Q-gtArray Q-gtRear X
ElementType Front( Queue Q ) if(
!IsEmpty( Q ) ) return Q-gtArray Q-gtFront
Error( "Empty queue" ) return 0 /
Return value used to avoid warning /
49Queue routines (queue.c contd)
void Dequeue( Queue Q ) if( IsEmpty( Q )
) Error( "Empty queue" ) else
Q-gtSize-- Q-gtFront Succ( Q-gtFront, Q
) ElementType FrontAndDequeue(
Queue Q ) ElementType X 0 if(
IsEmpty( Q ) ) Error( "Empty queue" ) else
Q-gtSize-- X Q-gtArray
Q-gtFront Q-gtFront Succ( Q-gtFront, Q
) return X
50Queue routines (queue.c contd)
include ltstdio.hgt include "queue.c" int main( )
Queue Q int i Q CreateQueue( 12 )
for( i 0 i lt 10 i ) Enqueue( i, Q )
while( !IsEmpty( Q ) ) printf(
"d\n", Front( Q ) ) Dequeue( Q )
DisposeQueue( Q ) return 0