The basic operation of the link stack (push, pop, and binary conversion) optimized version

#include<stdio.h>

#include<malloc.h>

#define ElemType int

typedef struct LinkStack {// definition of link stack structure, which is actually a single linked list limited  

ElemType data;  

struct LinkStack *next;

}LinkStack;

LinkStack *Init(LinkStack *top)

{// Initialize the link stack having a first node top = (LinkStack *) malloc (sizeof (LinkStack));

 top->next=NULL;  return top;

}

LinkStack *Push(LinkStack *top,ElemType &e)

{// push operation LinkStack * p;  

p=(LinkStack *)malloc(sizeof(LinkStack));

 if(p==NULL){   printf("栈满");  }

 else{   p->data=e;   p->next=top->next;   top->next=p;  }

 return top;

}

LinkStack *Pop(LinkStack *top)

{// pop operations LinkStack * p;  

ElemType and;

 p=top->next;

 if(p==NULL){   printf("栈空");  }  

else{  

 e=p->data;   

printf("%d",e);  

 top->next=p->next;   

free(p);  

}

 return top;

}

LinkStack *Get(LinkStack *top)

{// Get the top element, the element is still within the stack  

ElemType and;

 LinkStack *p;  

p=top->next;

 if(p==NULL){   printf("空栈");  

} Else {printf ( "\ n get the top element:");  

 e=p->data;   

printf("%d \n",e);  }

}

void *Coversion(ElemType dec)

{// Decimal octal ElemType e;  

LinkStack *top,*p;  top=Init(top);

 for(;dec!=0;dec=dec/8)

{   

e=dec%8;   top=Push(top,e);

 }  

for(p=top->next;p;p=top->next)

{  

 top=Pop(top);  }

}

int main ()

{  

LinkStack *top;

 ElemType dec;

 printf ( "Enter decimal number: \ n");

 scanf("%d",&dec);  

//top=Init(top);

 / * Printf ( "Please enter the stack elements:");  

scanf("%d",&e);  

while(e!=-1){   top=Push(top,e);   

scanf("%d",&e);  }  

Get(top);  

top=Pop(top);

 top=Pop(top);  

//Get_top(top);*/

 printf ( "for octal is:");

 Coversion(dec);  

return 0; }

 

Guess you like

Origin www.cnblogs.com/jiafeng1996/p/11306594.html
Recommended