目录
链表的应用:
- 栈
- 循环队列
- C语言实现动态数组
- 数组实现定长元素个数层次建树
- 队列实现不定元素个数层次建树 (*)
栈
栈(链表应用)
"stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tag{
int m_ival;
struct tag *next;
}Node,*pNode;
typedef struct{
pNode phead;//栈顶指针
int size;
}Stack,*pStack;
void initStack(pStack);
void pop(pStack);
void push(pStack,int);
int top(pStack);
int empty(pStack);
int size(pStack);
"stack.c"
#include "stack.h"
void initStack(pStack p)
{
memset(p,0,sizeof(Stack));
}
void pop(pStack p)
{
if(!p->size)
{
printf("stack is empty\n");
return;
}
p->phead=p->phead->next;
p->size--;
}
void push(pStack p,int val)
{
pNode pNew=(pNode)calloc(1,sizeof(Node));
pNew->m_ival=val;
if(NULL==p->phead)
{
p->phead=pNew;
}else{
pNew->next=p->phead;//新结点的next指向原有头结点
p->phead=pNew;
}
p->size++;
}
int top(pStack p)
{
return p->phead->m_ival;
}
int empty(pStack p)
{
return !p->size;
}
int size(pStack p)
{
return p->size;
}
"main.c"
#include "stack.h"
int main() {
Stack s;
initStack(&s);
push(&s, 5);
push(&s, 10);
printf("Stack size is %d\n", size(&s));
printf("Stack top val is %d\n", top(&s));
pop(&s);
printf("Stack top val is %d\n", top(&s));
pop(&s);
pop(&s);
return 0;
}
循环队列
queue.h
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 5
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int front,rear;
}SqQueue;
void InitQueue(SqQueue*);
int EnQueue(SqQueue*,ElemType);
int DeQueue(SqQueue*,ElemType*);
queue.c
#include "queue.h"
void InitQueue(SqQueue* pq)
{
pq->front=pq->rear=0;
}
int EnQueue(SqQueue* pq,ElemType val)
{
if((pq->rear+1)%MaxSize==pq->front)
{
printf("queue is full\n");
return 0;
}
pq->data[pq->rear]=val;
pq->rear=(pq->rear+1)%MaxSize;
return 1;
}
int DeQueue(SqQueue* pq,ElemType* x)
{
if(pq->rear==pq->front)
{
printf("queue is empty\n");
return 0;
}
*x=pq->data[pq->front];
pq->front=(pq->front+1)%MaxSize;
return 1;
}
main.c
#include "queue.h"
int main()
{
SqQueue q;
ElemType val;
InitQueue(&q);
EnQueue(&q,10);
EnQueue(&q,7);
EnQueue(&q,5);
EnQueue(&q,3);
EnQueue(&q,9);
DeQueue(&q,&val);
system("pause");
}
C语言实现动态数组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char *p;
int size;//当前放了多少数据
int capacity;//容量大小,申请了多少空间
}DynArr;
#define N 10
int main()
{
DynArr d;
int i=0;
d.size=0;
d.capacity=N;
d.p=(char*)malloc(d.capacity);
while(scanf("%c",d.p+d.size)!=EOF)
{
d.size++;
if(d.size==d.capacity)
{
d.capacity=d.capacity*2;
d.p=(char*)realloc(d.p,d.capacity);
}
}
d.p[d.size-1]='\0';
printf("%s\n",d.p);
system("pause");
}
队列实现不定元素个数层次建树 (*)
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct node{
ElemType c;
struct node *pleft;
struct node *prgiht;
}Node,*pNode;
typedef struct que{
pNode p;
struct que *pNext;
}Que_t,*pQue_t;
//前序遍历
void preOrder(pNode p)
{
if(p)
{
putchar(p->c);
preOrder(p->pleft);
preOrder(p->prgiht);
}
}
int main()
{
ElemType c;
pQue_t queHead=NULL,queTail=NULL,queNew,queFree;
pNode treeRoot=NULL,treeNew;
while(scanf("%c",&c)!=EOF)
{
if(c=='\n')
{
break;
}
treeNew=(pNode)calloc(1,sizeof(Node));//给树的新结点申请空间
treeNew->c=c;
queNew=(pQue_t)calloc(1,sizeof(Que_t));//给队列的新结点申请空间
queNew->p=treeNew;
if(!treeRoot)
{
treeRoot=treeNew;//第一个结点作为树根
queHead=queTail=queNew;//第一个结点作为队列头
}else{
if(NULL==queHead->p->pleft)
{
queHead->p->pleft=treeNew;
}else if(NULL==queHead->p->prgiht)
{
queHead->p->prgiht=treeNew;
//头部删除法
queFree=queHead;
queHead=queHead->pNext;
free(queFree);
}
//尾插法
queTail->pNext=queNew;
queTail=queNew;
}
}
preOrder(treeRoot);
printf("\n");
system("pause");
}
数组实现定长元素个数层次建树
#include<stdio.h>
#define N 10
typedef char ElemType;
typedef struct node {
ElemType c;
struct node *pleft;
struct node *pright;
}Node, *pNode;
void preOrder(pNode p)
{
if (p)
{
putchar(p->c);
preOrder(p->pleft);
preOrder(p->pright);
}
}
int main() {
ElemType c[N + 1] = "ABCDEFGHIJ";
int i, j;
pNode p[N]; //结构体指针数组
pNode root;
for (i = 0;i < N;++i) {
p[i] = (pNode)calloc(1, sizeof(Node));
p[i]->c = c[i];
}
root = p[0];
j = 0;
for (i = 1;i < N;++i) {
if (NULL == p[j]->pleft) {
p[j]->pleft = p[i];
}
else if (NULL == p[j]->pright) {
p[j]->pright = p[i];
j++;
}
}
preOrder(root);
return 0;
}