由栈和队列完成数组的逆置操作(C语言)

将数组a[11]={1, 3, 6, 10, 15, 16, 17, 18, 19, 20};  通过栈和队列实现元素逆置的算法,

入栈->出栈->入队->出队

#include"stdio.h"                                            
#include"stdlib.h"                                           
typedef int datatype;                                        
typedef struct node                                          
{                                                            
	datatype data;                                              
	struct node *next;                                          
}linkqene;                                                   
linkqene *push(linkqene *rear,datatype x)                    
{                                                            
	linkqene *s;                                                
	s=(linkqene *)malloc(sizeof(linkqene));                     
	s->data=x;                                                  
	s->next=NULL;                                               
	                                                            
    rear->next=s;                                            
    rear=s;                                                  
	return rear;                                                
}                                                            
int pop(linkqene *front,linkqene *rear)                      
{                                                            
	linkqene *p;                                                
	datatype  x;                                                
     if (rear==front) { printf("队空\n");exit(0);}           
     p=front->next;                                          
     x=p->data;                                              
     front->next=p->next;                                    
     if (p->next==NULL) rear=front;                          
     free( p);                                               
    return x;                                                
}                                                            
typedef  int datatype;                                       
typedef struct node1{                                        
       datatype  data;                                       
       struct node1 *next;                                   
   }linkstack;                                               
linkstack *top;                                              
int i=0,j=0;                                                 
linkstack *push1(linkstack *top,datatype x)                  
{                                                            
	linkstack *s;	                                              
 		s=(linkstack *)malloc(sizeof(linkstack));                 
 		s->data=x;                                                
 		s->next=top;                                              
 		top=s;	                                                   
	return top;	                                                
}                                                            
void pop2(linkstack *top)                                    
{                                                            
	datatype x;                                                 
	linkstack* p;                                               
	while(top)                                                  
	{                                                           
		p=top;                                                     
		x=p->data;                                                 
		printf("%d  ",x);                                          
		top=top->next;                                             
		free(p);                                                   
	}                                                           
	                                                            
}                                                            
main()                                                       
{                                                            
	int x,x1=0,i;                                               
	linkqene *rear,*front;                                      
	front=(linkqene *)malloc(sizeof(linkqene));                 
	front->next=NULL;                                           
	rear=front;	                                                
	int a[11]={1, 3, 6, 10, 15, 16, 17, 18, 19, 20};            
	for(i=0;i<10;i++)                                           
	{                                                           
		int x=a[i];                                                
	rear=push(rear,x);                                          
	}                                                           
	linkstack *top=NULL;                                        
	datatype x3;                                                
	puts("入队成功");                                           
	for(i=0;i<10;i++)                                           
	{                                                           
	x1=pop(front,rear);                                         
	top=push1(top,x1);                                          
	}                                                           
	puts("入栈成功");                                           
	puts("倒序输出为");                                         
	pop2(top);	                                                 
}                                                            

 

 

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值