CSC 301 Sem1 2021-2022 - 2 Data Structures I
CSC 301 Sem1 2021-2022 - 2 Data Structures I
Algorithms
Denis L. Nkweteyim
Outline
●
Fundamental Data Types Review
●
User-defined Data Types
●
Functions Review
●
Pointers
●
Structures
●
Arrays and dynamic memory allocation
●
Linked lists
●
Stacks and queues
●
Strings
union Data {
int i;
float f;
char str[20];
};
Output
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
10/15/21 D.L. Nkweteyim: CSC 301@UB - Data Structures I 17
data.str : C Programming
Union
●
And why there are no output errors from
the following code
#include <stdio.h>
#include <string.h>
union Data {
int i; float f; char str[20];
};
int main( ) {
union Data data;
data.i = 10; printf( "data.i : %d\n", data.i);
data.f = 220.5; printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
}
Output
data.i : 10
data.f : 220.500000
data.str : C Programming
10/15/21 D.L. Nkweteyim: CSC 301@UB - Data Structures I 18
Typedef
●
Used to associate an identifier with a
particular type, for example
●
In the example below, the type enum day is given an
alternative name, day. Note that there is no conflict with
the identifier day, which is handled quite separately from
the type enum day
●
Notes
–
Call from function main() to another function print_hello()
–
Function prototype
–
Function definition that holds the code that makes up the
function.
10/15/21 D.L. Nkweteyim: CSC 301@UB - Data Structures I 23
Functions Example 1
●
Good programing practice to declare all user-defined functions before they
are used
–
This is done through prototypes
●
Prototype syntax
–
type function_name (parameter type list);
●
Type
–
Data type that the function returns (keyword void used if the function does not
return a value)
●
Parameter type list
–
Comma separated list of the data types that are passed to the function
●
If no parameter is passed, the keyword void is used
–
Often, for the sake of clarity, the types in the parameter type list are followed by
identifiers which the compiler does not use
–
Example: the following two function prototypes are equivalent
●
void func(char c, int i);
●
void func(char, int);
int a, b;
int *p;
a = 5; b = 4; p = &a; //p points to address of a
printf("*p = %d\n", *p); /* *p = 5 */
printf("a = %d\n", a); /* a = 5 */
*p = 20;
printf("*p = %d\n", *p); /* *p = 20 */
printf("a = %d\n", a); /* a = 20 */
struct stu_rec {
int id;
char name[15];
int score;
};
struct stu_rec pers1, pers2;
typedef struct {
int id;
char name[15];
int score;
} stu_rec;
typedef struct {
float x;
float y;
} point;
float distance(point, point);
int main(void) {
point a, b;
a.x = 5; a.y = 10;
b.x = -5; b.y = 5;
printf("Distance between (%.2f,%.2f) and (%.2f,%.2f) is
%.2f\n",a.x,a.y,b.x,b.y,distance(a,b));
}
float distance(point a, point b) {
float dx = a.x - b.x, dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
●
Nodes
–
Most times, for sake of simplicity, we
shall only use simple data on node
●
See example below right
–
Important to remember that in real
life, nodes are much more complex
data structures than simple data
types
struct list a, b, c; a b c
a.data = 1; b.data = 2; c.data = 3; 1 NULL 2 NULL 3 NULL
a.next = b.next = c.next = NULL;
1 2 3 NULL
●
Problem with linked list definition above
–
One variable for each node
●
Too tedious especially if there are several nodes in the list
–
More common (and convenient) to dynamically
allocate the memory that a linked list needs
Student ID: 4
Score I: 45
Score II: 66
Student ID: 7
Score I: 39
Score II: 56
Student ID: 6
Score I: 57
Score II: 55
x
10/15/21 D.L. Nkweteyim: CSC 301@UB - Data Structures I 69
Deletion from a doubly linked list
●
Given a node to be deleted, t
–
Set t -> next -> prev to t -> prev
–
Set t -> prev -> next to t -> next
josephus 9 5
1 2 3 4 5 6 7 8 9
Nodes eliminated: 6 2 8 5 4 7 1 3
Winner is 9
josephus 20 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Nodes eliminated: 4 7 10 13 16 19 2 6 11 15 20 5 12 18 8 17 9 3 14
Winner is 1
In the Indian city of Benares, beneath a dome that marked the center of the
world, is to be found a brass plate in which are set three diamond needles,
"each a cubit high and as thick as the body of a bee." Brahma placed 64 disks
of pure gold on one of these needles at the time of Creation. Each disk is a
different size, and each is placed so that it rests on top of another disk of
greater size, with the largest resting on the brass plate at the bottom and
the smallest at the top. Within the temple are priests whose job it is to
transfer all the gold disks from their original needle to one of the others,
without ever moving more than one disk at a time. No priest can ever place any
disk on top of a smaller one, or anywhere else except on one of the needles.
When the task is done, and all 64 disks have been successfully transferred to
another needle, "tower, temple, and Brahmins alike will crumble into dust, and
with a thunder-clap the world will vanish." The prediction (thunder-clap
aside) seems fairly safe given that the number of steps required to transfer
all the disks is 2^64 - 1, which is approximately 1.8447×10^19. Assuming one
second per move, this would take about five times longer than the current age
of the universe!
hanoi disk
227 300 380 560 649 694 805 1086
10/15/21 D.L. Nkweteyim: CSC 301@UB - Data Structures I 99