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));
}
}