您的位置:首页 > 其它

栈和队列的有关操作

2016-04-24 10:10 417 查看
直接上代码:

#include <stdio.h>
#include <stdlib.h>
#define  Stack_Init_Size  100
#define  Max_Size  100
struct node {
int res;
struct node *next;
};

struct node *Init_List_Stack(){    //对链栈的初始化,进栈,出栈操作
struct node *head;
head=(struct node *)malloc(sizeof(struct node));
if(!head)
return NULL;
head->next=NULL;
return head;
}

void Push_List_Stack(int x,struct node *head){
struct node *p,*q;
p=(struct node *)malloc(sizeof(struct node));
p->res=x;
q=head;
while(q->next){
q=q->next;
}
q->next=p;
p->next=NULL;
}

void Pop_List_Stack(struct node *head){
struct node *q,*tmp;
q=head;
while(q->next->next){
q=q->next;
}
tmp=q->next;
q->next=tmp->next;
free(tmp);
}

void Read_List_Stack(struct node *head){
while(head->next){
printf("%d ",head->next->res);
head=head->next;
}
printf("\n");
}
struct Sq_Stack{
int *base;
int *top;
int stacksize;
};

void Init_Sq_Stack(struct Sq_Stack &s){   //对顺序栈的初始化,进栈,出栈操作
s.base=(int *)malloc(Stack_Init_Size*sizeof(int));
if(!s.base)
printf("内存开辟失败\n");
s.top=s.base;
s.stacksize=Stack_Init_Size;
}

void Push_Sq_Stack(struct Sq_Stack &s,int x){
if(s.top-s.base>=Stack_Init_Size)
printf("栈已满\n");
else{
*(s.top)=x;
s.top++;
}
}

void Pop_Sq_Stack(struct Sq_Stack &s){
if(s.top==s.base)
printf("栈已空\n");
else
s.top--;
}

void Read_Sq_Stack(struct Sq_Stack s){
while(s.top!=s.base){
printf("%d ",*(s.base++));
}
printf("\n");
}

struct  Qnode{
int res;
struct Qnode *next;
};

struct  Link_Queue{
struct Qnode *front;
struct Qnode *rear;
};

void Init_Link_Queue(struct Link_Queue &s){    //对链队列的初始化,进队,出队操作
s.front=(struct Qnode*)malloc(sizeof(struct Qnode));
if(!s.front)
printf("开辟内存失败\n");
s.rear=s.front;
s.front->next=NULL;
}

void Enter_Queue(struct Link_Queue &s,int x){
struct Qnode *p;
p=(struct Qnode*)malloc(sizeof(struct Qnode));
p->res=x;
p->next=NULL;
s.rear->next=p;
s.rear=p;
}

void Delete_Queue(struct Link_Queue &s){
struct Qnode *p;
if(s.front==s.rear)
printf("队列已空\n");
p=s.front->next;
s.front->next=p->next;
if(p==s.rear)
s.rear=s.front;
free(p);
}

void Read_Queue(struct Link_Queue s){
while(s.front->next){
printf("%d ",s.front->next->res);
s.front=s.front->next;
}
printf("\n");
}

struct Circle_Queue{
int *base;
int front;
int rear;
};

void Init_Circle_Queue(struct Circle_Queue &s){    //对循环队列的初始化,进队,出队操作
s.base=(int *)malloc(Max_Size*sizeof(int));
if(!s.base)
printf("内存开辟失败\n");
s.front=s.rear=0;
}

void Enter_Circle_Queue(struct Circle_Queue &s,int x){
if((s.rear+1)%Max_Size==s.front)
printf("循环队列已满\n");
s.base[s.rear]=x;
s.rear=(s.rear+1)%Max_Size;
}

void Delete_Circle_Queue(struct Circle_Queue &s){
if(s.front==s.rear)
printf("循环队列已空\n");
s.front=(s.front+1)%Max_Size;
}

void Read_Circle_Queue(struct Circle_Queue s){
int i;
for(i=s.front;i<s.rear;i++)
printf("%d ",s.base[i]);
printf("\n");
}

void  Conversion(int data,int num){   //利用栈来实现进制转换
struct Sq_Stack s;
int res;
Init_Sq_Stack(s);
while(data){
Push_Sq_Stack(s,data%num);
data=data/num;
}
while(s.base!=s.top){
res=*(--s.top);
if(res>=10)
printf("%c",(char)(65+res-10));
else
printf("%d",res);
}
printf("\n");
}

void Print_YHSJ(int n){           //利用队列来打印杨辉三角
struct Circle_Queue t;
int i,j;
int q1,q2;
Init_Circle_Queue(t);
Enter_Circle_Queue(t,0);
Enter_Circle_Queue(t,1);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
Enter_Circle_Queue(t,0);
do
{
q1=t.base[t.front];
Delete_Circle_Queue(t);
q2=t.base[t.front];
Enter_Circle_Queue(t,q1+q2);
if(q2)
printf("%d ",q2);
}
while(q2);
printf("\n");
}
}
int main()
{
struct node *head;
struct Sq_Stack  s;
struct Link_Queue r;
struct Circle_Queue t;
int data,num;
int n,x,i;
printf("链栈的基本操作     \n");

printf("请输入要入栈节点的个数\n");
head=Init_List_Stack();
scanf("%d",&n);
printf("请输入节点中的数据\n");
for(i=1;i<=n;i++){
scanf("%d",&x);
Push_List_Stack(x,head);
}
printf("完整的栈中数据\n");
Read_List_Stack(head);
printf("将栈中栈顶元素依次出栈\n");
for(i=1;i<=n;i++){
Pop_List_Stack(head);
Read_List_Stack(head);
}

printf("顺序栈的基本操作     \n");

Init_Sq_Stack(s);
printf("请输入要入栈节点的个数\n");
scanf("%d",&n);
printf("请输入要入栈的数据\n");
for(i=1;i<=n;i++){
scanf("%d",&x);
Push_Sq_Stack(s,x);
}
printf("完整的栈中数据\n");
Read_Sq_Stack(s);
printf("将栈中栈顶元素依次出栈\n");
for(i=1;i<=n;i++){
Pop_Sq_Stack(s);
Read_Sq_Stack(s);
}

printf("链队列的基本操作     \n");

printf("请输入要入队列的个数\n");
Init_Link_Queue(r);
scanf("%d",&n);
printf("请输入队列节点中的数据\n");
for(i=1;i<=n;i++){
scanf("%d",&x);
Enter_Queue(r,x);
}
printf("完整的队列中数据\n");
Read_Queue(r);
printf("将队列中元素依次出队\n");
for(i=1;i<=n;i++){
Delete_Queue(r);
Read_Queue(r);
}

printf("循环队列的基本操作     \n");

printf("请输入要入队列的个数\n");
Init_Circle_Queue(t);
scanf("%d",&n);
printf("请输入队列节点中的数据\n");
for(i=1;i<=n;i++){
scanf("%d",&x);
Enter_Circle_Queue(t,x);
}
printf("完整的队列中数据\n");
Read_Circle_Queue(t);
printf("将循环队列中元素依次出队\n");
for(i=1;i<=n;i++){
Delete_Circle_Queue(t);
Read_Circle_Queue(t);
}

printf("请输入要转换的数\n");
scanf("%d",&data);
printf("输入要转换的进制\n");
scanf("%d",&num);
Conversion(data,num);

printf("请输入要转换的数\n");
scanf("%d",&data);
printf("输入要转换的进制\n");
scanf("%d",&num);
Conversion(data,num);

printf("输出杨辉三角\n");

printf("输入杨辉三角的行数\n");
scanf("%d",&n);
Print_YHSJ(n);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: