数据结构知识的遗忘 发表于 2018-03-11 | 分类于 数据结构 今天对链表,堆栈,队列,二叉树东西进行了总结: 链表123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129/*结构*/typedef struct node{ int data; struct node *next;}node ,*link;/*创建,逆序创建*/link creath(){ node *l; l=(node *)malloc(sizeof(node)); l->next=NULL; int x; while(~scanf("%d",&x)) { node *p; p=(node *)malloc(sizeof(node)); p->data=x; p->next=l->next; l->next=p; } return l;}/*创建,顺序创建*/link creatt(){ node *l,*r; l=(node *)malloc(sizeof(node)); l->next=NULL; r=l; int x; while(~scanf("%d",&x)) { node *p; p=(node *)malloc(sizeof(node)); p->data=x; p->next=NULL; r->next=p; r=p; } r->next=NULL; return l;}/*删除*/link linkdelete(link l,int x){ link p,pre; p=l->next; while(p->data!=x) { pre=p; p=p->next; } pre->next=p->next; free(p); return l;}``` ### 堆栈:``` bash/*结构*/typedef struct{ int *base; int *top; int stacksize;}sqstack;/*创建一个空栈*/int init(sqstack &s){ s.base=(int *)malloc(15*sizeof(int)); s.top=s.base; s.stacksize=15; return 1;}/*销毁栈*/int destroy(sqstack &s){ s.top=NULL; s.stacksize=0; free(s.base); return 1;}/*清空栈*/int empy(sqstack s){ if(s.top==s.base) { return 0; } else { return 1; }}/*求栈的长度*/int stacklength(sqstack s){ if(s.top==s.base) { return 0; } else { return(s.top-s.base); }}/*求栈顶元素*/int gettop(sqstack s,int &e){ if(s.top==s.base) { return 0; } else { e=*(s.top-1); } return e;}/*栈顶插入元素*/int push(sqstack s,int &e){ *s.top=e; s.top++; return 1;} 堆栈,队列相关c++1234567891011121314/*堆栈*/stack s;s.push(x) //入栈s.pop() //出栈s.top() //取栈顶s.empty() //判断是否为空;s.size() //判断堆栈的长度;/*队列*/queue q;q.push(x) //入队q.pop() //出队q.front() //队首元素q.back() //队尾元素q.empty() //判断队是否为空 二叉树123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354#include <iostream>using namespace std;typedef struct node{ char data; struct node *lchild,*rchild;}*bitree,node;void create(bitree &t){ char ch; cin>>ch; if(ch=='#') t=NULL; else { t=new node; t->data=ch; create(t->lchild); create(t->rchild); }}void inorder(bitree t){ if(t) { inorder(t->lchild); cout<<t->data; inorder(t->rchild); }}/*树的深度*/int depth(bitree t){ if(t==NULL) { return 0; } else { int m=depth(t->lchild); int n=depth(t->rchild); if(m>n) return m+1; else return n+1; }}/*节点个数*/int nodecount(bitree t){ if(t==NULL) return 0; else return nodecount(t->lchild)+nodecount(t->rchild)+1;} 二叉树通过两种遍历的方式(其中中序遍历一定已知),得知另一种遍历的方式123已知先序遍历与中序遍历解题思路:*******中序遍历里一个字母的两边是左右子树****************后续遍历的特点是给一棵树的根是最后访问的******