算法导论 数据结构
栈 ;先进后出;羽毛球筒,第一个放进去,最后一个拿出来,球桶只有一个开口。
队列;先进先出;羽毛球筒,两个开口,一个进口,一个出口。
#include<stdio.h>
#include<assert.h>
#define N 6
typedef struct queue SQ;//队列
struct queue{
int array[N];
int* head;
int* tail;
};//申明队列结构体
int enqueue(SQ* p, int n);//入队
int dequeue(SQ* p);//出队
int main() {
SQ Q = { {0},Q.array,Q.array };//Q队列初始化
int n, k = 0, tmp[N+1] = { 0 };
while (~scanf("%d", &n)) {//入队操作
if(enqueue(&Q,n)==-1)
break;
}
while (1) {
tmp[k++] = dequeue(&Q);//出队操作
if (tmp[k - 1] == -1)
{
tmp[k - 1] = 0;
break;
}
}
}
int enqueue(SQ* p, int n) {
*p->tail = n;
p->tail++;
if (p->tail == &p->array[N]) {
p->tail = &p->array[0];
}//尾部循环
if (p->head == p->tail)//判断溢出
return -1;
}
int dequeue(SQ* p) {
if (p->head == &p->array[N]) {
p->head = &p->array[0];
}//尾部循环
if (p->head == p->tail) //判断溢出
return -1;
return *(p->head++);
}
#include<stdio.h>
#include<malloc.h>
typedef struct link SL;
struct link {
int n;
SL* head;
SL* tail;
};
int main() {
SL array = { 0,NULL };
int n;
SL* p = &array;
SL* tmp = p;
while (~scanf("%d", &n)) {
p->n = n;
p->tail = (SL*)malloc(sizeof(SL));
p->tail->head = p;
p = p->tail;
}
p->tail=NULL;
p = &array;
while (p->tail!=NULL) {
printf("%d\n", p->n);
p = p->tail;
}
}
队列;先进先出;羽毛球筒,两个开口,一个进口,一个出口。
链表;第一元素包含着本身的值第二个元素的地址,以此类推,元素相互关联。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
栈代码走起
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#define N 10
typedef struct stack SS;//栈
int push(SS *p,int n);//压栈
int pop(SS* p);//出栈
struct stack {//栈
int array[N];
int top;
};
int main()
{
SS S1;
S1.top = 0;
int n,len,i=0;
while(~scanf("%d", &n)) {
len=push(&S1, n);//对S1压入数据
assert(len <= N);//断言
}
int array[N] = { 0 };
while (S1.top > 0) {//对S1提取数据
array[i++] = pop(&S1);
}
}
int push(SS *p, int n) {
p->array[p->top] = n;
return ++p->top;
}
int pop(SS* p) {
return p->array[--p->top];
}
#include<stdio.h>
#include<assert.h>
#define N 10
typedef struct stack SS;//栈
int push(SS *p,int n);//压栈
int pop(SS* p);//出栈
struct stack {//栈
int array[N];
int top;
};
int main()
{
SS S1;
S1.top = 0;
int n,len,i=0;
while(~scanf("%d", &n)) {
len=push(&S1, n);//对S1压入数据
assert(len <= N);//断言
}
int array[N] = { 0 };
while (S1.top > 0) {//对S1提取数据
array[i++] = pop(&S1);
}
}
int push(SS *p, int n) {
p->array[p->top] = n;
return ++p->top;
}
int pop(SS* p) {
return p->array[--p->top];
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
队列代码走起
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<assert.h>
#define N 6
typedef struct queue SQ;//队列
struct queue{
int array[N];
int* head;
int* tail;
};//申明队列结构体
int enqueue(SQ* p, int n);//入队
int dequeue(SQ* p);//出队
int main() {
SQ Q = { {0},Q.array,Q.array };//Q队列初始化
int n, k = 0, tmp[N+1] = { 0 };
while (~scanf("%d", &n)) {//入队操作
if(enqueue(&Q,n)==-1)
break;
}
while (1) {
tmp[k++] = dequeue(&Q);//出队操作
if (tmp[k - 1] == -1)
{
tmp[k - 1] = 0;
break;
}
}
}
int enqueue(SQ* p, int n) {
*p->tail = n;
p->tail++;
if (p->tail == &p->array[N]) {
p->tail = &p->array[0];
}//尾部循环
if (p->head == p->tail)//判断溢出
return -1;
}
int dequeue(SQ* p) {
if (p->head == &p->array[N]) {
p->head = &p->array[0];
}//尾部循环
if (p->head == p->tail) //判断溢出
return -1;
return *(p->head++);
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
双链链表代码走起
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<malloc.h>
typedef struct link SL;
struct link {
int n;
SL* head;
SL* tail;
};
int main() {
SL array = { 0,NULL };
int n;
SL* p = &array;
SL* tmp = p;
while (~scanf("%d", &n)) {
p->n = n;
p->tail = (SL*)malloc(sizeof(SL));
p->tail->head = p;
p = p->tail;
}
p->tail=NULL;
p = &array;
while (p->tail!=NULL) {
printf("%d\n", p->n);
p = p->tail;
}
}
算法导论 文章被收录于专栏
以小白的角度诠释算法导论,假如有人看不懂,我就面壁思过。