你會發現上面的例子中, 如果struct裡面有pointer to struct, 而我們想要用該pointer來存取結構成員時, 就必須很小心的用*和()來表達。由於結構成員包括指向結構的指標(define a pointer to struct in a struct), 是很常見的事情, 這樣的(*(*listOne.next).next).data語法既難寫又難懂, 因此C語言定義了->運算符號。此符號的左邊是一個pointer to struct, 右邊則是該pointer指到的結構成員。->為第一優先權左結合, 因此
void pushStack(STACK *s, int y) {
ITEM *x; // x will point to the new ITEM
x = (ITEM *) malloc(sizeof(ITEM)); // allocate memory for the new ITEM
x->data = y; // store data
x->link = s->top; // x->link points to where s->top points
s->top = x; // stack's top points to x
}
int popStack(STACK *s) {
ITEM * x = s->top;
int d = x->data;
s->top = s->top->link;
free(x);
return d;
}
int stackIsEmpty(STACK *s) {
return s->top == NULL;
}
void main() {
STACK s;
int i;
initStack(&s);
for (i = 1; i < 10; i++) {
pushStack(&s, i);
}
while (!stackIsEmpty(&s)) {
printf("%d\n", popStack(&s));
}
}
Linked Queue
typedef struct items {
int data;
struct items *link; // points to next element
} ITEM;
void initQueue(QUEUE *q) {
q->size = 0;
q->front = q->rear = NULL;
}
int queueIsEmpty(QUEUE *q) {
return q->front == NULL;
}
int queueLength(QUEUE *q) {
return q->size;
}
void addQueue(QUEUE *q, int y) {
ITEM * x = (ITEM *) malloc(sizeof(ITEM));
x->data = y;
x->link = NULL;
if (q->front == NULL)
q->front = x;
else
q->rear->link = x;
q->rear = x;
q->size++;
}
int deleteQueue(QUEUE *q) {
ITEM * x = q->front;
int rel = x->data;
q->front = x->link;
if (q->front == NULL)
q->rear = NULL;
q->size--;
free(x);
return rel;
}
void main() {
QUEUE q;
int i;
initQueue(&q);
for (i = 1; i < 10; i++) {
addQueue(&q, i);
}
while (!queueIsEmpty(&q)) {
printf("%d\n", deleteQueue(&q));
}
}
以下範例定義了矩陣結構,並透過動態空間分配的方式來做矩陣的加法和乘法
/**
* Author: Shiuh-Sheng Yu
* Department of Information Management
* National Chi Nan University
* Subject: 矩陣相加與相乘
* Toolkit: gcc
* Modified Date:2002/08/20
*/
#include <stdio.h>
// 以巨集(macro)定義矩陣元素和動態分配空間的對應關係
// 所謂巨集指的是經由preprocessor(前置處理器)取代原始檔內的字串
#define M(x,i,j) *(x->data + i*x->col + j)
// 定義MATRIX為 struct matrix *
// 也就是說MATRIX之型態為 a pointer to struct matrix
// 至於struct則是C語言讓使用者 "自訂型態" 的關鍵字
typedef struct matrix {
int row, col;
double* data;
} *MATRIX;
/**
* 由檔案讀入一個矩陣
*/
MATRIX readMatrix(FILE* f) {
int x, y, i, j;
char keyword[256];
MATRIX m;
/* read in keyword "matrix" */
fscanf(f, "%255s", keyword);
if (strcmp(keyword,"matrix")!=0) {
printf("keyword error: %s",keyword);
return NULL;
}
// 動態分配一塊struct matrix大小的空間
m = (MATRIX) malloc(sizeof(struct matrix));
/* read in matrix dimension to x y */
fscanf(f,"%d", &x);
fscanf(f,"%d", &y);
m->row = x;
m->col = y;
m->data = (double*)malloc(x * y * sizeof(double));
/* read in x*y double and store them to m->data */
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
fscanf(f,"%lf",m->data + i*y + j);
}
}
return m;
}
/**
* 印出矩陣的內容
*/
void printMatrix(MATRIX x) {
int i, j;
for (i = 0; i < x->row; i++) {
for ( j= 0; j < x->col; j++) {
printf("%lf", M(x,i,j));
}
printf("\n");
}
}
/**
* 矩陣相加
* 傳回一新矩陣為x,y之和
*/
MATRIX addMatrix(MATRIX x, MATRIX y) {
int i, j;
MATRIX m;
// 檢查兩矩陣的大小是否能相加
if ((x->row != y->row) || (x->col != y->col)) {
printf("Matrix dimension mismatch.\n");
return NULL;
}
// 產生新矩陣所需的記憶體空間
m = (MATRIX) malloc(sizeof(struct matrix));
m->row = x->row;
m->col = x->col;
//產生存放資料所需的空間
m->data = (double*)malloc(m->row * m->col * sizeof(double));
// 進行矩陣的加法運算
for (i = 0; i < m->row; i++) {
for (j = 0; j < m->col; j++) {
M(m,i,j) = M(x,i,j) + M(y,i,j); // 使用macro
}
}
return m;
}
MATRIX multiplyMatrix(MATRIX x, MATRIX y) {
/* 自己練習看看吧 */