链表操作函数(C程序).doc_第1页
链表操作函数(C程序).doc_第2页
链表操作函数(C程序).doc_第3页
链表操作函数(C程序).doc_第4页
链表操作函数(C程序).doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

链表操作函数(C程序).txt18拥有诚实,就舍弃了虚伪;拥有诚实,就舍弃了无聊;拥有踏实,就舍弃了浮躁,不论是有意的丢弃,还是意外的失去,只要曾经真实拥有,在一些时候,大度舍弃也是一种境界。/main.c文件#include #include #includequeue.h#includestack.hint main() struct Node * p=NULL; struct Node * p1=NULL,*p2=NULL;/定义三个结构体,并将头指针都置为NULL int a, c; printf(*菜单*n); printf( 1:创建链表 2:销毁 3:压栈 4:弹栈 5:显示出栈数据 6:判空 7:置空 8:计数 ); printf(n); printf( 9:创建队列 10:销毁队列 11:一个数据入队列 12:一个数据出队列 0:退出 ); printf(n); printf(*n); printf(请选择操作:);/以上为整个工程要实现的目录菜单 scanf(%d,&c); while (1) switch(c)/使用switch case循环来测试整个工程 case 1: p=Create(); printf(链表创建成功!n); break; case 2: Destory(p); p=NULL; printf(栈已被完全销毁!n); break; case 3: printf(请输入数据:); scanf(%d,&a); Push(a,p); break; case 4: Pop(p); printf(数据已被弹出!n如果要显示弹出的数据,请选择菜单5n); break; case 5: Display(p); break; case 6: a=IsEmpty(p); if (a=1) printf(栈是空的n); else printf(栈不是空的n); break; case 7: Clear(p); printf(栈已被清空!n); break; case 8: printf(共%d个数据n,Count(p); break; case 9: printf(队列创建成功!n); CreateQueue(p1,p2); break; case 10: printf(队列已被完全销毁!n); DestoryQueue(p1,p2); break; case 11: EnQueue(a); printf(该数据已成功的进入队列中!n); break; case 12: printf(该数据已经出队列!n); break; case 0: return 0; break; default: return 0; break; printf(请选择操作:); scanf(%d,&c); return 0;/Queue.h文件#ifndef QUEUE_H_INCLUDED#define QUEUE_H_INCLUDED typedef struct Node int data; struct Node* pNext; Node;void CreateQueue(Node*h1,Node* h2);/创建void DestoryQueue(Node* h1, Node* h2);/销毁void EnQueue(int data); /一个数据入队列int DeQueue(Node* h1, Node* h2); /一个数据出队列#endif / QUEUE_H_INCLUDED/Queue.c文件#include #include #includequeue.h/调用对应的.h文件void CreateQueue(Node* h1,Node* h2)/创建队列 h1=Create();/调用两个栈来模拟队列 h2=Create();void DestoryQueue(Node* h1, Node* h2)/销毁队列 Destory(h1);/将两个模拟对列的栈都销毁 Destory(h2);void EnQueue(int data)/一个数据入队列 Node* h1; int j; j = h1-data; h1 = (Node *)malloc(sizeof(Node);/将一个数压入其中一个栈中,即模拟了一个数据入队列 if(h1 = NULL) printf(不能分配内存空间!);/判断一下是否内存申请成功 printf(请输入一个入队列的数据:); scanf(%d,&j); h1-pNext = NULL;int DeQueue( Node* h1,Node* h2)/一个数据出队列 Node*pNew; if(IsEmpty(h1)&IsEmpty(h2)/判断一下队列是否为空 printf(队列里无数据); else if(IsEmpty(h2) while(h1-pNext!=NULL) for(pNew=h1;pNew-pNext!=NULL;pNew=pNew-pNext); Push(pNew-data,h2); Pop(h1); Pop(h2);/最终将数据输出队列 /Stack.h文件#ifndef STACK_H_INCLUDED#define STACK_H_INCLUDEDNode* Create();/创建void Destory(Node* head);/销毁void Push(int data,Node* head);/压栈int Pop(Node* head);/弹栈int IsEmpty(Node* head);/判空void Clear(Node* head);/置空int Count(Node* head);/计数void Display(Node* head);/显示#endif / STACK_H_INCLUDED/Stack.c文件#include #include typedef struct Node int data; struct Node* pNext;Node;/定义结构体#includeStack.h/调用对应的头文件Node* Create()/创建 Node* head ; head=(Node *)malloc(sizeof(Node);/为头指针申请一块内存 if(head=NULL)/判断是否申请成功 printf(不能分配内存空间!); head - pNext = NULL;/将指针变量初始化 return head;void Destory(Node* head)/销毁 Node*pNew,*ppNew; pNew=head; while(pNew!=NULL)/使用while循环删除栈中每一个结点 ppNew= pNew; pNew=pNew-pNext; free(ppNew);/再将栈底free掉,即实现销毁 void Push(int data,Node*head)/压栈 Node*pNew,*next; if(head=NULL)/首先需要判断一下栈是否为空 printf(请先创建堆栈n); goto end; for(pNew=head;pNew-pNext!=NULL;pNew=pNew-pNext);/运用for循环操作不断增加链栈的结点数 pNew-pNext=malloc(sizeof(Node);/并每次将指针pNew指向下一结点的内存 if(pNew-pNext=NULL)/ printf(不能分配内存空间!n);/判断是否申请成功 goto end; next=pNew-pNext; next-data=data; next-pNext=NULL; end:;int Pop(Node* head)/弹栈 int a; Node*pNew; if(head=NULL) exit(1); for(pNew=head;pNew-pNext!=NULL;pNew=pNew-pNext); a = pNew-data; free(pNew-pNext); pNew-pNext=NULL; return a;int IsEmpty(Node* head)/判空 return (head-pNext=NULL)?1:0;/一个判断语句即可实现void Clear(Node* head)/置空 Node*pNew,*ppNew; pNew=head; while(pNew!=NULL)/使用while循环连续删除栈中每一个结点 ppNew= pNew; pNew=pNew-pNext; free(ppNew); head-pNext=NULL;/防止产生野指针将head-pNext指向空int Count(Node* head)/计数 Node*pNew; int n; for(pNew=head;pNew!=NULL;pNew=pNew-pNext)/使用for循环即可

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论