CSC 302 Linear Data Structure Implementation Using C
CSC 302 Linear Data Structure Implementation Using C
Overview
1. Introduction to Pointers
2. Introduction to structures
3. Stack Implementation
4. Queue Implementation
Pointers: An Introduction
• Recall that the declaration int x=11; causes a C compiler to reserve,
statically, a cell of size two bytes and place the value 11 there.
• A detail we have not mentioned up to now is that apart from occupying
some space, each variable has also an address in memory.
• The address of a variable is important because it can be used to manipulate
the value(s) of the variable more efficiently.
• The address of a variable is usually implicitly referenced by the compiler
and can also be explicitly referenced by the programmer in C.
• A pointer variable is a variable that holds the address of another variable.
• Does a pointer variable have an address, too?
Pointers: Example 1
#include <stdio.h>
void main()
{
int count = 10, x, *ip;
ip = &count;
x = *ip;
#include <stdio.h>
void main()
{
int i1, i2, *p1, *p2;
i1 = 5;
p1 = &i1;
i2 = *p1 / 2 + 10;
p2 = p1;
void main()
{
int a[] = {2,3,5,7,11,13,17,19};
int *p = &a[5];
for (; p != a; p--)
printf("%d\n", *p);
}
For example:
#include <stdio.h>
void main()
{
int m[4][3] = {{10,5,-3}, {9, 0, 0},
{32,20,1}, {7,0,8}};
int row, column, sum;
sum = 0;
for(row = 0; row < 4; row++ )
for(column = 0; column < 3; column++ )
sum = sum + *(*(m+row)+column);
printf("The total is %d\n", sum );
}
• Note that the arrangement of array elements (2-D and more) into rows and
columns is only conceptually true; there are no rows and columns in
memory.
• Hence 2-D array elements are arranged linearly in memory like (a base
address 3002 is assumed for m):
• All that the compiler remembers about a 1-D array is its base address. If we
consider a 2-D array m[4][3], the base addresses of the four 1-D
arrays1 would be stored in m[0],m[1],m[2],m[3].
• Naturally, the expression m[2]+1 would give the address of the second
element in the third 1-D array. The value at this address can be obtained by
the expression *(m[2]+1) or *(*(m+2)+1.
• In general, the 2-D array element m[i][j] can be accessed more efficiently
using pointers as *(*(m+i)+j).
Overview
1. Structures: A Motivation
2. Tools for Creating Structures
3. Declaring and Initialising Structures
4. Accessing and Modifying Structure Members
5. Preview: Structures (cont.)
Structures: Motivations
• Our programs so far used only the basic data types provided by C, i.e., char, int, float,
void and pointers.
• We are now at a more advanced stage where programmers can define their own data
types using structures
• Structures are types that represent structured collections of data pertaining to
particular objects
• Structures can be used to conveniently model things that would otherwise be difficult
to model using arrays
• Arrays versus structures:
o arrays elements and structure components are contiguously laid out in memory
o arrays are homogeneous, structures can be heterogeneous
o arrays are passed to functions by reference while structures have to be
explicitly passed by reference
• different structures (in the same program) can have the same component name. A
structure component name can also be the same as an ordinary variable name
main()
{
struct date today;
today.day = 17;
today.month = 11;
today.year = 1998;
Manipulating Structures
#include <stdio.h>
yaum.month = 3;
yaum.year = 1999;
printf("The date is %d/%d/%d.\n",
yaum.day, yaum.month, yaum.year );
}
main()
{
struct date today;
today.day = 16;
today.month = 7;
today.year = 1998;
printf("The date is %d/%d/%d.\n",
today.day, today.month, today.year );
modify_date(today);
main()
{
struct date dates[5];
int i;
main()
{
POINT p;
printf("Please enter two integers:");
scanf("%d%d", &p.x, &p.y);
Overview
1. Arrays of Structures
2. Arrays of Structures using Pointers
3. Preview: Abstract Data Types (ADT) Concepts
Arrays of Structures: Example 1
Write a program to enter in five dates, Store this information
in an array of structures.
#include <stdio.h>
struct date {
int day, month, year;
};
main()
{
struct date dates[5];
int i;
Overview
1. The ADT Stack
2. The Stack Operations
3. Modeling the ADT Stack using an Array
4. A Simple Application: Reversing a Line of Text
5. Preview: Queue ADT
The ADT Stack
• An ADT, in general, is a non-implemented data structure
with a set of operations that can be used to access the
data structure
• The ADT specifies only the abstract or logical properties
of the data structure and access functions, leaving the
implementation to the programmer
• In the ADT stack, items are added/removed from the
same stack location called the stack top.
• The following operations define the applications
programmer's (i.e., user's) interface to the ADT stack:
1. createStack() 2. destroyStack() 3. isFullStack()
4. isEmptyStack() 5. pushStack() 6. popStack()
CreateStack(&stack);
while (!StackFull(&stack) &&
(item = getchar()) != '\n')
Push(item, &stack);
while (!StackEmpty(&stack)) {
Pop(&item, &stack);
putchar(item);
}
putchar('\n');
}
Overview
1. The ADT Queue
2. Possible Array-Based Representations
3. Queue ADT: Implementation Using Circular Array
The ADT Queue
• Like the array and the stack, the queue is also a
homogeneous collection of objects
• The difference between a stack and queue lies in the way
they are accessed
• While the stack is a LIFO structure, the queue is a FIFO
structure
• While a stack has one access point, the top, the queue has
two access points: the front where objects are removed
and the rear where objects are added
• The following operations define the applications
programmer's (i.e., user's) interface to the ADT queue:
1. createQueue() 2. destroyQueue() 3. isFullQueue()
4. isEmptyQueue() 5. enqueueItem() 6. dequeueItem()
• Note that both the stack and the queue are specialised
form of the linked list (to be discussed in the next few
lectures)
• Both the stack and the queue have many practical
applications in computer science. For example, the stack
in used in compilers and the queue is often used in
scheduling algorithms
Queue ADT: Circular Array Implementation
#include<stdio.h>
#define MAX_QUEUE 100
typedef char QueueEntry;
typedef enum {FALSE, TRUE} Boolean;
main()
{
QueueType queue;
createQueue(&queue);
inputQueue(&queue);
outputQueue(&queue);
destroyQueue(&queue);
return;
}