第五周项目2-链栈算法库
来源:互联网 发布:淘宝流量在哪里看手机 编辑:程序博客网 时间:2024/06/06 02:06
通过学习书上和他人的思维模式,感觉非常有收获;需要做的工作有很多,我要争分夺秒!
main.cpp
#include<bits/stdc++.h>#include"lianzhan.h"using namespace std;int main(){ int a,b,c,d,i; linknode *p;///并未在主函数中对p分配一块内存空间// cout<<&p<<endl;// cout<<p<<endl; chushi(p); a=zhanempty(p); cout<<a<<endl; jinzhan(p,1); jinzhan(p,2); jinzhan(p,3); jinzhan(p,4); jinzhan(p,5); b=zhanempty(p); cout<<b<<endl; c=zhanchang(p); cout<<c<<endl; display(p); for(i=0;i<4;i++) { cout<<chuzhan(p)<<' '; } cout<<chuzhan(p)<<endl; d=zhanempty(p); cout<<d<<endl; //cout<<&p<<endl; xiao(p); return 0;}
#ifndef LIANZHAN_H_INCLUDED#define LIANZHAN_H_INCLUDEDtypedef struct node{ int data; node *next;}linknode;void chushi(linknode *&p);void xiao(linknode *&p);int zhanempty(linknode *p);void jinzhan(linknode *&p,int n);int chuzhan(linknode *&p);int quding(linknode *p);void display(linknode *p);int zhanchang(linknode *p);#endif // LIANZHAN_H_INCLUDED
lianzhan.cpp
#include<bits/stdc++.h>#include"lianzhan.h"using namespace std;void chushi(linknode *&p)///初始化{// cout<<&p<<endl;// cout<<p<<endl; p=(struct node *)malloc(sizeof(struct node));// cout<<p<<endl; p->next=NULL;}//void xiao(linknode *&p)///销毁栈//{// linknode *r,*pre=p;// r=p->next;// while(r!=NULL)// {// free(pre);// pre=r;// r=pre->next;// }// free(pre);//}void xiao(linknode *&p)///销毁栈{ //cout<<&p<<endl; linknode *r; r=p->next; while(r!=NULL) { free(p); p=r; r=p->next; } free(r);}int zhanempty(linknode *p)///判断栈是否为空{ return (p->next==NULL);}void jinzhan(linknode *&p,int n)///进栈{ linknode *q; q=(struct node *)malloc(sizeof(struct node)); q->data=n; q->next=p->next; p->next=q;}int chuzhan(linknode *&p)///出栈!到了链式结构上,竟然没有反应过来哪里是头,哪里是尾{ if(p->next==NULL) { cout<<"栈为空"<<endl; exit(0); } linknode *t; int f; t=p->next; f=t->data; p->next=t->next; free(t); return f;}int quding(linknode *p)///取栈顶元素{ if(p->next==NULL) { cout<<"栈为空"<<endl; exit(0); } return p->next->data;}void display(linknode *p){ p=p->next; while(p->next!=NULL) { cout<<p->data<<' '; p=p->next; } cout<<p->data<<endl;}int zhanchang(linknode *p){ linknode *t; t=p; int ch=0; t=t->next; while(t->next!=NULL) { ch++; t=t->next; } return ch+1;}
运行结果:
知识点总结:
知识点都是类似的,但是总会有新想法和新解法;积极并包容性地学习!对于这个链栈,因为使用了标准化写法与我自己的写法混合型的写法,所以就出现了一些错误;出现错误还是很正常的,不要怕错误,迎难而上!在这个过程中虽然没学到什么新知识,但是我对老知识又有了新的更进一步的理解,初步掌握了一种解决问题的新思路!
心得体会:
少说多做!
阅读全文
0 0
- 第五周项目2--建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周 项目2-建立链栈算法库
- 第五周项目2 -建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周 项目2-建立链栈算法库
- 第五周项目2 -建立链栈算法库
- 第五周 项目2 - 建立链栈算法库
- 第五周--项目2-建立链栈算法库
- 第五周 项目2 - 建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周项目2--建立链栈算法库
- 第五周项目2 --建立链栈算法库
- 第五周项目2—建立链栈算法库
- 第五周--项目2 建立链栈算法库
- P1438 无聊的数列
- CodeForces E. Lucky Array 幸运数列
- P2023 [AHOI2009]维护序列 --线段树
- 洛谷 P2253 好一个一中腰鼓 --线段树
- [HNOI2004] L语言 --trie树
- 第五周项目2-链栈算法库
- [NOI2000] 单词查找树
- 关于字符串的整理
- P1470 最长前缀 Longest Prefix
- HDU How far away ?--LCA
- P2922 [USACO08DEC]秘密消息Secret Message
- P2420 让我们异或吧
- 51Nod 1526 分配 笔名
- 图书管理系统full-stack总结