/**
单链表的有关操作(Shubao Lv)
⑴随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序)。
⑵遍历单向链表。
⑶把单向链表中元素逆置(不允许申请新的结点空间)。
⑽在主函数中设计一个简单的菜单,分别调试上述算法。
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include <malloc.h>
using namespace std;
struct note
{
int num;
note *next;
};
note *h;
///建立单链表
void creatLink()
{
printf("输入0结束(第一个数不能为0)\n");
note *p1,*p2;
p1=(struct note *)malloc(sizeof(struct note));
h=NULL;
p1->next=NULL;
scanf("%d",&p1->num);
h=p1;
p2=p1;
p1=(struct note *)malloc(sizeof(struct note));
p2->next=p1;
int x;
scanf("%d",&x);
while(x)
{
p1->num=x;
p2=p1;
p1=(struct note *)malloc(sizeof(struct note));
p2->next=p1;
scanf("%d",&x);
}
p2->next=NULL;
}
///遍历单链表
void bianli()
{
note *p;
for(p=h;p!=NULL;p=p->next)
printf("%d ",p->num);
puts("");
}
///倒置单链表
void daozhi()
{
note *pre,*cur,*nex;
pre=h;
cur=h->next;
nex=NULL;
while(cur!=NULL)
{
nex=cur->next;
cur->next=pre;
pre=cur;
cur=nex;
}
h->next=NULL;
h=pre;
bianli();
}
int main()
{
creatLink();
/*srand(time(NULL));
int n=rand()%255+1;*/
bianli();
daozhi();
return 0;
}
/**
栈和队列的基本操作(Shubao Lv)
⑴采用链式存储实现栈的初始化、入栈、出栈操作。
⑵采用顺序存储实现栈的初始化、入栈、出栈操作。
⑶采用链式存储实现队列的初始化、入队、出队操作。
⑷采用顺序存储实现循环队列的初始化、入队、出队操作。
⑸在主函数中设计一个简单的菜单,分别测试上述算法。
*⑹综合训练:①利用栈实现表达式求值算法。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define INIT_SIZE 100
#define INCREASEMENT 10
using namespace std;
const int INF = -0xfffffff;
struct Strack
{
int *top;///栈顶的指针
int *base;///栈底的指针
int stracksize;
void init()///栈的初始化
{
base=(int *) malloc(INIT_SIZE * sizeof(int ));///分配内存
top=base;
stracksize = INIT_SIZE;
}
int Top() ///返回栈顶元素
{
if(top==base)
return INF;
return *(top-1);
}
bool pop() ///删除栈顶元素
{
if(top==base)
return false;
top--;
return true;
}
void push(int x) ///栈顶插入元素
{
if(top-base>=stracksize) ///如果内存不够重新分配内存
{
base=(int *)realloc(base,(stracksize+INCREASEMENT)*(sizeof(int )));
top=base+stracksize;
stracksize+=INCREASEMENT;
}
*top++=x;
}
bool empty() ///判断栈是否为空
{
if(top==base)
return true;
return false;
}
}s;
struct Queue
{
int *top;///队顶的指针
int *base;///队底的指针
int queuesize;
void init()///队的初始化
{
base=(int *) malloc(INIT_SIZE * sizeof(int ));///分配内存
top=base;
queuesize = INIT_SIZE;
}
int Top() ///返回队顶元素
{
if(top==base)
return INF;
return *top;
}
bool pop() ///删除队顶元素
{
if(top==base)
return false;
top++;
return true;
}
void push(int x) ///队顶插入元素
{
if(base-top>=queuesize) ///如果内存不够重新分配内存
{
base=(int *)realloc(base,(queuesize+INCREASEMENT)*(sizeof(int )));
base=top+queuesize;
queuesize+=INCREASEMENT;
}
*base++=x;
}
bool empty() ///判断队是否为空
{
if(top==base)
return true;
return false;
}
}q;
int main()
{
///==============栈的部分================
int x;
s.init();
cout<< "1、进栈:\n(输入要进栈的元素,以0结束)\n";
while(1)
{
int x;
scanf("%d",&x);
if(x==0)
break;
s.push(x);
}
cout<<"出栈:\n";
while(!s.empty())
{
printf("%d ",s.Top());
s.pop();
}
puts("");
///============队列的部分================
q.init();
cout<< "\n2、入队:\n(输入要入队的元素,以0结束)\n";
while(1)
{
int x;
scanf("%d",&x);
if(x==0)
break;
q.push(x);
}
cout<<"出队:\n";
while(!q.empty())
{
printf("%d ",q.Top());
q.pop();
}
puts("");
return 0;
}
<pre class="cpp" name="code">#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
/**
二叉树的有关操作(Shubao Lv)
⑴输入字符序列,建立二叉链表。
⑵中序遍历二叉树:递归算法。
⑶中序遍历二叉树:非递归算法。(最好也能实现先序,后序非递归算法)
(4)正则二叉树是二叉树中不存在子树个数为一的结点,即节点的度为2.
判断一棵树是正则二叉树的算法
⑼在主函数中设计一个简单的菜单,分别调试上述算法。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define NULL 0
#define N 10
int sum;
struct node
{
int data;
node *lchild,*rchild;
};
node *creat_tree()
{
node *t;
int x;
cin >> x;
if(x!=0)
{
t=new node;
t->data=x;
t->lchild=creat_tree();
t->rchild=creat_tree();
}
else
t=NULL;
return t;
}
///递归先序遍历二叉树
void preorder(node *t)
{
if(t)
{
cout << t->data<<" ";
preorder(t->lchild);
preorder(t->rchild);
}
}
///非递归先序遍历算法
void preorder1(node *t)
{
node * s[N],*p;
int top;
top=0;
p=t;
while(p || top>0)
{
while(p)
{
cout<<p->data<<" ";
top++;
s[top]=p;
p=p->lchild;
}
if(top>0)
{
p=s[top];
--top;
p=p->rchild;
}
}
}
///非递归中序遍历二叉树
void inorder(node *t)
{
node *p,*s[N];
int top;
top=0;
p=t;
while(p || top>0)
{
while(p)
{
top++;
s[top]=p;
p=p->lchild;
}
if(top>0)
{
p=s[top];
top--;
cout<<p->data<<" ";
p=p->rchild;
}
}
}
///二叉树后根非递归算法
void pasorder1(node *t)
{
struct
{
node *pp;
int tag;
} ss[N];
int top;
node *p;
top=0;
p=t;
while(p || top>0)
{
while(p)
{
top++;
ss[top].tag=0;
ss[top].pp=p;
p=p->lchild;
}
if(top>0)
if(ss[top].tag==0)
{
ss[top].tag=1;
p=ss[top].pp;
p=p->rchild;
}
else
{
p=ss[top].pp;
cout<<p->data<<" ";
top--;
p=NULL;
}
}
}
///递归算法判断是否是正则二叉树
void count_leaf(node *t)
{
if(t)
{
if((t->lchild!=NULL && t->rchild==NULL)||(t->lchild==NULL && t->rchild!=NULL))
sum=0;
count_leaf(t->lchild);
count_leaf(t->rchild);
}
}
int main()
{
int num;
node *t;
t=creat_tree();
cout << "前序遍历:\n";
preorder(t);
cout <<endl;
cout << " 中序遍历:\n";
inorder(t);
cout <<endl;
/**pasorder1(t);
cout <<endl;
preorder1(t);
cout <<endl;*/
cout <<"是否为正则二叉树:\n";
sum=1;
count_leaf(t);
if(sum)
cout<<"YES\n";
else
cout <<"NO\n";
return 0;
}
/**
1
2
4
0
0
5
0
0
3
6
0
0
7
0
0
1
2
4
0
0
0
3
5
0
0
6
0
0
*/