C语言强化——链表(2)
目录
链表的应用:
- 栈
- 循环队列
- 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;
}
C语言强化——链表(2)的更多相关文章
- C语言强化——链表(1)
目录 链表的增删(不带头结点) 链表相关面试题 合并两个有序链表 单链表原地逆置 找出链表的倒数第四个节点 找出链表的中间节点 判断单链表是否有环 求链表交点 删除有序单链表中重复的元素 链表按奇数. ...
- C语言之链表
这两天在复习C语言的知识,为了给下个阶段学习OC做准备,以下的代码的编译运行环境是Xcode5.0版本,写篇博文把昨天复习的C语言有关链表的知识给大家分享一下,以下是小菜自己总结的内容,代码也是按照自 ...
- C语言习题 链表建立,插入,删除,输出
Problem B: C语言习题 链表建立,插入,删除,输出 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 222 Solved: 92 [Subm ...
- YTU 2430: C语言习题 链表建立,插入,删除,输出
2430: C语言习题 链表建立,插入,删除,输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 576 解决: 280 题目描述 编写一个函数creatlink,用来建立一个动态链表 ...
- 关于c语言单项链表尾添加
犹豫了几天,看了很多大牛写的关于c语言链表,感触很多,终于下定决心,把自己对于链表的理解随之附上,可用与否,自行裁夺.由于作者水平有限也是第一次写,不足之处,竭诚希望得到各位大神的批评指正.制作不易, ...
- C语言之链表list
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h& ...
- C语言:链表实现的一个实例
问题:写一个程序输入你一年看过的所有电影以及每部电影的各种信息(简化问题:每部电影只要求输入片名和评价) 链表实现: #include<stdio.h> #include<stdli ...
- C语言单链表实现19个功能完全详解
谢谢Lee.Kevin分享了这篇文章 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将 ...
- (转)c语言_链表实例讲解(两个经典例子)
建立一个学生成绩的线性链表,对其实现插入,删除,输出,最后销毁. #include <stdio.h>#include <stdlib.h> struct grade { ...
随机推荐
- LeetCode - Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- nginx实现集群高可用
大家知道NGINX作为反向代理服务器可以实现负载均衡,同时也可以作为静态文件服务器,它的特点就是并发支持大,单机可同时支持3万并发,现在很多网站都把NGINX作为网关入口来统一调度分配后端资源.但是如 ...
- export的变量另开一个终端失效解决方法
有时候,我们需要把一个export的变量全局话,否则每开一个终端又需要重新export,很是麻烦 首先直接export某个变量的话就只能在当前子终端生效,另开一个终端就失效了 如果修改.bash_pr ...
- IBM WebSphere MQ介绍安装以及配置服务详解
首先介绍一下MQ MQ消息队列的简称是一种应用程序对应用程序的通信方法.说白了也就是通过队列的方式来对应用程序进行数据通信.而无需专用链接来链接它们. MQ的通讯方式 1.数据报的方式 Datagra ...
- nginx+php windows安装配置
https://blog.csdn.net/zjiang1994/article/details/72876193 https://blog.csdn.net/bruce_wang_janet/art ...
- 深入浅出:MySQL的左连接、右连接、内连接
http://blog.csdn.net/wyzxg/article/details/7276979 三种连接的语法 为便于更多的技友快速读懂.理解,我们只讨论2张表对象进行连接操作的情况,大于2张表 ...
- Mysql主从---删除master.info和relya-log.info实验
relay-log.info, master.info 这连个文件时在建立复制时产生的,现在主要说明以下问题: 1.如果修改删除master.info文件,复制会中断么? 不会,如果stop slav ...
- react 子组件访问父组件的方法
回调函数(推荐) 地址:https://ourcodeworld.com/articles/read/409/how-to-update-parent-state-from-child-compone ...
- 相似度与距离计算python代码实现
#定义几种距离计算函数 #更高效的方式为把得分向量化之后使用scipy中定义的distance方法 from math import sqrt def euclidean_dis(rating1, r ...
- Centos6.8 Mysql5.6 安装配置教程(转)
准备Mysql文件: 方式一:wget url(mysql下载地址); 方式二:从官网下载mysql,上传至centos(小编使用的Nodepad++的NppFTP具体做法请百度); 检查之前安装 ...