pushc語言
① 怎樣用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));
}
}
② 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(" ");
}
③ 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;
}
④ 關於嚴蔚敏C語言版數據結構的棧PUSH實現代碼
ElemType是筆誤S.base=(ElemType *)malloc (S.base, (S.stacksize+STACKINCREMENT)*sizeof(Elemtype));這個是分配一段內存,長度是(S.stacksize+STACKINCREMENT)*sizeof(Elemtype)這么多位元組,因為這個函數是重新分配的,所以也要分配表s.base的存儲空間