c语言push
1. C语言 push和pop函数可以直接用吗
#include<stdio.h>
#include<stdlib.h>
#defineMAXSIZE32
typedefstruct{
int*elem;/*栈的存储区*/
intmax;/*栈的容量,即找中最多能存放的元素个数*/
inttop;/*栈顶指针*/
}Stack;
intInitStack(Stack*S,intn)/*创建容量为n的空栈*/
{
S->elem=(int*)malloc(n*sizeof(int));
if(S->elem==NULL)return-1;
S->max=n;
S->top=0;//栈顶初值0
return0;
}
intPush(Stack*S,intitem)/*将整数item压入栈顶*/
{
if(S->top==S->max){
printf("Stackisfull! ");
return-1;
}
S->elem[S->top++]=item;//压栈,栈顶加1
return0;
}
intStackEmpty(StackS)
{
return(!S.top)?1:0;/*判断栈是否为空*/
}
intPop(Stack*S)/*栈顶元素出栈*/
{
if(!S->top){
printf("Popanemptystack! ");
return-1;
}
returnS->elem[--S->top];//弹出栈,栈顶减1
}
voidMultibaseOutput(longn,intB)
{
intm;StackS;
if(InitStack(&S,MAXSIZE)){
printf("Failure! ");
return;
}
do{
if(Push(&S,B))//------
{
printf("Failure! ");
return;
}
n=n-1;//--------
}while(n!=0);
while(!StackEmpty(S)){/*输出B进制的数*/
m=Pop(&S);
if(m<10)printf("%d",m);/*小于10,输出数字*/
elseprintf("%c",m+55);/*大于或等于10,输出相应的字符*/
}
printf(" ");
}
2. 求助数据结构题用C语言做
1: 因为要删除那些即在B表又在C表中的元素,所以A,B,C三个表中都会有这个元素。那么用指针遍历A表,用另外两个指针遍历B,C。查找B,C中同A的元素,因为3个表都是有序的,可以采用些简单的比较。找到后删除。
2:void AE(stack &s)
{
int stack (s); //得到传递过来的栈
push(s,3); // 3进栈
push(s,4); // 4进栈
int x=pop(s)+2*pop(s); // x = 3 + 2 * 4
push(s,x); // x 进栈
int a[5]={2,5,8,22,15};
for(j=0;j<5;j++)
push(s,a[i]) // A数组进栈
while(!stackempty(s)) // 直到栈空
printf("%d",2*pop(s)); //输出 2*栈中每个元素
结果自己想了。
3. 我一直使用C语言写单片机程序,不太清楚程序进入中断后,一系列PUSH……,到底是把ACC,PSW等
PUSH指令是把所要保存的数据存到内部数据存储器里,也就是内部RAM,具体哪个地址要看SP堆栈指针的指向,如果默认SP为07H的话,那么存的空间地址就从08H开始。这些值都是可以用直接寻址调用的,想干什么都行。但记着在中断程序返回前把它们按照顺序POP回原来的寄存器里。
4. C语言用push和pop来写单链表倒序程序
//单链表的逆序输出分为两种情况,一种是只逆序输出,实际上不逆序;另一种是把链表逆序//本文就分别实例讲述一下两种方法。具体如下:
//1.逆序输出
//实例代码如下:
#include<iostream>
#include<stack>
#include<assert.h>
usingnamespacestd;
typedefstructnode{
intdata;
node*next;
}node;
//尾部添加
node*add(intn,node*head){
node*t=newnode;
t->data=n;
t->next=NULL;
if(head==NULL){
head=t;
}
elseif(head->next==NULL){
head->next=t;
}
else{
node*p=head->next;
while(p->next!=NULL){
p=p->next;
}
p->next=t;
}
returnhead;
}
//顺序输出
voidprint(node*head){
node*p=head;
while(p!=NULL){
cout<<p->data<<"";
p=p->next;
}
cout<<endl;
}
//递归
voidreversePrint(node*p){
if(p!=NULL){
reversePrint(p->next);
cout<<p->data<<"";
}
}
//栈
voidreversePrint2(node*head){
stack<int>s;
while(head!=NULL){
s.push(head->data);
head=head->next;
}
while(!s.empty()){
cout<<s.top()<<"";
s.pop();
}
}
intmain(){
node*head=NULL;
for(inti=1;i<=5;i++){
head=add(i,head);
}
print(head);
reversePrint(head);
reversePrint2(head);
system("pause");
return0;
}
//2.单链表逆序
//实例代码如下:
#include<iostream>
#include<stack>
#include<assert.h>
usingnamespacestd;
typedefstructnode{
intdata;
node*next;
}node;
node*add(intn,node*head){
node*t=newnode;
t->data=n;
t->next=NULL;
if(head==NULL){
head=t;
}
elseif(head->next==NULL){
head->next=t;
}
else{
node*p=head->next;
while(p->next!=NULL){
p=p->next;
}
p->next=t;
}
returnhead;
}
//循环
node*reverse(node*head){
if(head==NULL||head->next==NULL){
returnhead;
}
node*p1=head;
node*p2=head->next;
node*p3=NULL;
head->next=NULL;
while(p2!=NULL){
p3=p2;
p2=p2->next;
p3->next=p1;
p1=p3;
}
head=p1;
returnhead;
}
voidprint(node*head){
node*p=head;
while(p!=NULL){
cout<<p->data<<"";
p=p->next;
}
cout<<endl;
}
//递归
node*reverse2(node*p){
if(p==NULL||p->next==NULL){
returnp;
}
node*newHead=reverse2(p->next);
p->next->next=p;
p->next=NULL;
returnnewHead;
}
intmain(){
node*head=NULL;
for(inti=1;i<=5;i++){
head=add(i,head);
}
print(head);
head=reverse(head);
print(head);
head=reverse2(head);
print(head);
system("pause");
return0;
}
5. 怎样用C语言写出对栈进行的五种运算:push()、pop()、top()、empty()、makempty()
这是我用链表写的:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int x;
struct node *next;
}Node;
typedef struct stack
{
Node *top;
}Stack;
void InitStack(Stack *s)
{
s->top=NULL;
}
int IsEmpty(Stack *s)
{
if(s->top==NULL)
return 1;
else
return 0;
}
void PushStack(Stack *s,int x)
{
Node *p;
p=(Node*)malloc(sizeof(Node));
p->x=x;
// p->next=NULL;
p->next=s->top;
s->top=p;
}
int PopStack(Stack *s)
{
int data;
Node *p;
p=(Node *)malloc(sizeof(Node));
if(IsEmpty(s))
{
printf("the stack is empty!\n");
free(p);
return -1;
}
else
{
p=s->top;
data=p->x;
s->top=p->next;
free(p);
return data;
}
}
int main (int argc,char **argv)
{
int i;
Stack s;
InitStack(&s);
for(i=0;i<1000;i++)
{
PushStack(&s,i);
}
for(i=0;i<1000;i++)
{
printf("%d\n",PopStack(&s));
}
}