C語言資料結構拉練 佇列,堆疊

2021-10-10 18:42:45 字數 2928 閱讀 3757

資料結構 = 結構定義 + 結構操作

#include

#include

#include

#define color(a, b) "\033[" #b "m" a "\033[0m"

#define green(a) color(a, 32)

(1)陣列, (2)隊首, 隊尾, (3)佇列容量, (4)佇列長度

typedef

struct queue queue;

初始化/清除

queue *

init

(int n)

void

clear

(queue *q)

顯示隊首, 佇列空標識

int

front

(queue *q)

intempty

(queue *q)

迴圈佇列擴容

採用malloc, 不建議使用realloc

malloc返回值為null即為開闢新空間失敗

int

expand

(queue* q)if(

!p)return0;

for(

int i = q->head, j =

0; j < q->length;

++i,

++j)

free

(q->data)

; q->data = p;

q->size +

= extr_size;

q->head =0;

q->tail = q->length;

return1;

}

入隊/出隊/顯示

int

push

(queue* q,

int val)

q->data[q->tail]

= val;

q->tail++;if

(q->tail == q->size) q->tail =0;

q->length++

;return1;

}int

pop(queue* q)

void

output

(queue* q)

printf

("}\n");

return

;}

int

main()

break

;case3:

break;}

output

(q);

}#undef max_op

clear

(q);

return0;

}

#include

#include

#include

#define color(a, b) "\033[" #b "m" a "\033[0m"

#define green(a) color(a, 32)

(1) 陣列, (2)棧頂, (3) 容量, (4)長度

typedef

struct stack stack;

初始化/清除

stack *

init

(int n)

void

clear

(stack* s)

讀出棧頂, 棧空標識

int

top(stack* s)

intempty

(stack* s)

棧空間擴容

使用realloc時預先設定相關型別指標,以免原資料丟失

int

expand

(stack* s)if(

!extr_size)

return0;

s->data = p;

s->size +

= extr_size;

return1;

}

入棧/出棧/顯示

int

push

(stack* s,

int val)

s->data[

++(s->top)

]= val;

return1;

}int

pop(stack *s)

void

output

(stack* s)

printf

("}\n");

return

;}

int

main()

break

;case3:

break;}

output

(s);

}clear

(s);

return0;

}

C語言資料結構拉練 並查集

資料結構 結構定義 結構操作 結構定義 陣列 尺寸 數值相同的陣列索引標記為一類 include include typedef struct unionset unionset 結構操作 初始化 清除 unionset init int n return u void clear unionset...

資料結構 堆疊 C語言

1.基本概念 定義 限定只能在固定一端進行插入和刪除操作的線性表。特點 後進先出。允許進行插入和刪除操作的一端稱為棧頂,另一端稱為棧底。作用 可以完成從輸入資料序列到某些輸出資料序列的轉換 1 順序堆疊 順序堆疊 順序儲存結構的堆疊。順序棧的儲存結構 利用一組位址連續的儲存單元依次存放自棧底到棧頂的...

C語言資料結構拉練 順序表,鍊錶

資料結構 結構定義 結構操作 include include include 標註擴容操作的顏色巨集 033 32m列印值 033 0m define color a,b 033 b m a 033 0m define green a color a,32 1 陣列指標,2 容量,3 長度 type...