阅读背景:

1基础-4链栈

来源:互联网 
#include<iostream> #include<stdlib.h> using namespace std; typedef struct Node{ int data; struct Node* next; }Node; void push(Node *&head,int x){//输入头指针引用与入栈值 Node *s=(Node*)malloc(sizeof(Node));//开新结点 s->data=x;//赋值 s->next=head->next;//赋后继 head->next=s;//更新头指针 } int pop(Node *head){//输入头指针 if(head->next==NULL)//头结点后继为空表示空栈 return 0;//返回0删除失败 Node *tmp=head->next;//读出栈顶元素 head->next=head->next->next;//更新头指针至其后继 free(tmp);//删除原头指针 return 1; } int main(){//链栈,本例程也是默认有头结点不存值 Node* head=(Node*)malloc(sizeof(Node));//初始化头结点 push(head,100);push(head,200);push(head,300);//入栈三连 cout<<head->next->data<<endl;pop(head);//读栈顶元素并弹出 cout<<head->next->data<<endl;pop(head);//读栈顶元素并弹出 cout<<head->next->data<<endl;pop(head);//读栈顶元素并弹出 return 0; } /*** 300 200 100 ***/ #include<iostream> #include<stdlib.h> using names



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: