Introduction DS and Structures
Introduction DS and Structures
to
Data Structures
Definition
Data structure
Primitive DS Non-Primitive DS
Non-Primitive DS
Head
AAA BBB CCC
Information field Pointer field
Lists
PUSH POP
[STACK]
Stack
front rear
Queue
A root
B C
D E F G
Graph
Example of graph:
6
v2 v5
v1 v3
10
v1 8 11
15
9 v2
v3 v4 v4
Types of Graphs:
• Directed graph
• Undirected graph
• Simple graph
• Weighted graph
• Connected graph
• Non-connected graph
Storage class in C
Topics
Automatic variables
External variables
Static variables
Register variables
Scopes and longevity of above types of
variables.
28
Few terms
1. Scope: the scope of a variable determines over
what part(s) of the program a variable is actually
available for use(active).
2. Longevity: it refers to the period during which a
variables retains a given value during execution
of a program(alive)
3. Local(internal) variables: are those which are
declared within a particular function.
4. Global(external) variables: are those which are
declared outside any function.
29
Automatic variables
Are declared inside a function in which they are to be
utilized and declared using a keyword auto.
eg. auto int number;
Are created when the function is called and destroyed
automatically when the function is exited.
30
Example program
int main()
{ int m=1000;
function2();
printf(“%d\n”,m);
}
function1()
{
int m = 10;
printf(“%d\n”,m);
}
function2() Output
{ int m = 100; 10
function1(); 100
printf(“%d\n”,m); 1000
}
31
Few observation about auto variables
It is visible
33
only from the point of declaration to the end of the
program.
External variable (examples)
int number; int count;
float length=7.5; main()
main() {count=10;
{ . . . . . .
. . . . . .
} }
funtion1() funtion()
{. . . {int count=0;
. . . . . .
} . . .
funtion1() count=count+1;
{. . . }
. . .
} When the function
The variables number references the variable
and length are available count, it will be referencing
for use34in all three only its local variable, not
the global one.
Global variable example
int x; int fun3()
{
int main() x=x+10;
{ return(x);
x=10; }
printf(“x=%d\n”,x); Once a variable has been
printf(“x=%d\n”,fun1());
printf(“x=%d\n”,fun2());
declared global any function
printf(“x=%d\n”,fun3()); can use it and change its
} value. The subsequent
int fun1() functions can then reference
{ x=x+10;
return(x);
only that new value.
} Output
int fun2() x=10
{ int x
x=1; x=20
return(x);
}
x=1
35
x=30
External declaration
int main()
{ • As far as main is concerned, y is
not defined. So compiler will issue
y=5;
. . . an error message.
. . . • There are two way out at this point
}
int y; 1. Define y before main.
2. Declare y with the storage class
func1() extern in main before using it.
{
y=y+1
}
36
External declaration(examples)
int main()
{
extern int y;
. . .
. . .
Note that extern declaration
}
func1() does not allocate storage
{ space for variables
extern int y;
. . .
. . .
}
int y;
37
Multifile Programs and extern variables
file1.c file2.c
file1.c file2.c
41
Examples (internal static)
int main()
{
int I;
for(i =1; i<=3; i++)
stat();
}
void stat() Output
{
x=1
static int x=0;
x = x+1; x=2
printf(“x = %d\n”,x);
x=3
}
42
External static variables
43
Static function
C structures and
unions
47
C structures: aggregate, yet
scalar
aggregate in that they hold multiple data items at
one time
named members hold data items of various types
like the notion of class/field in C or C++
– but without the data hiding features
scalar in that C treats each structure as a unit
as opposed to the “array” approach: a pointer to a
collection of members in memory
entire structures (not just pointers to structures) may be
passed as function arguments, assigned to variables,
etc.
Interestingly, they cannot be compared using ==
(rationale: too inefficient)
48
Structure declarations
Combined variable and type declaration
struct tag {member-list} variable-list;
Any one of the three portions can be omitted
Structure declarations
struct S {int a, b; char *p;}; /* omit
variables */
No variables are declared, but there is now a type struct
S that can be referred to later
Member access
Direct access operator s.m
• subscript and dot operators have same precedence
and associate left-to-right, so we don’t need
parentheses for sam.pets[0].species
Indirect access s->m: equivalent to (*s).m
• Dereference a pointer to a structure, then return a
member of that structure
• Dot operator has higher precedence than indirection
operator , so parentheses are needed in (*s).m
(*fido.owner).name or
fido.owner->name . and -> have equal
. evaluated first: access owner precedence and associate
member
* evaluated next: dereference pointer to
left-to-right
53
Memory layout
struct COST { int amount;
char currency_type[2]; }
struct PART { char id[2];
struct COST cost;
int num_avail; }
layout of struct PART:
currency_type
id amount num_avail
cost
Memory layout
A better alternative (from a space perspective):
struct COST { int amount;
char currency_type; }
struct PART { struct COST cost;
char id[2];
int num_avail;
}
currency_type
amount id num_avail
cost
55
Bit fields
Bit field members must be
ints
If space is a serious concern, you can select the number
of bits used for each member Note: This won’t work
on machines with 16-bit
struct CHAR { unsigned ch: 7; ints
unsigned font: 6;
unsigned size: 19; };
Layout possibilities (machine-dependent):
ch font size
size font ch
56
Bit fields
Portability is an issue:
• Do any bit field sizes exceed the machine’s int size?
• Is there any pointer manipulation in your code that
assumes a particular layout?
Bit fields are “syntactic sugar” for more complex
shifting/masking
• e.g. to get font value, mask off the ch and size
bits, then shift right by 19
• This is what actually happens in the object code –
bit fields just make it look simpler at the source level
57
Structures as function
arguments
Structures are scalars, so they can be returned and
passed as arguments – just like ints, chars
struct BIG changestruct(struct BIG s);
• Call by value: temporary copy of structure is created
• Caution: passing large structures is inefficient
– involves a lot of copying
avoid by passing a pointer to the structure instead:
void changestruct(struct BIG *s);
What if the struct argument is read-only?
• Safe approach: use const
void changestruct(struct BIG const *s);
58
Unions
Like structures, but every member
occupies the same region of memory!
• Structures: members are “and”ed together:
“name and species and owner”
• Unions: members are “xor”ed together
union VALUE {
float f;
int i;
char *s;
};
/* either a float xor an int xor a string */
59
Unions
Up to programmer to determine how to
interpret a union (i.e. which member to
access)
Often used in conjunction with a “type”
variable that indicates how to interpret the
union value
Access type to
determine how
enum TYPE { INT, FLOAT, STRING };
to interpret
struct VARIABLE {
value
enum TYPE type;
union VALUE value;
};
60
Unions
Storage
• size of union is the size of its largest member
• avoid unions with widely varying member
sizes;
for the larger data types, consider using
pointers instead
Initialization
• Union may only be initialized to a value
appropriate for the type of its first member
61