Programming implementation of stack/stack operations

Source: Internet
Author: User

The complete code is as follows, but the team stack is just a change in the list.

#include <stdio.h>#include<stdlib.h>typedefstructStudent *Pnode;typedefstructStacklink *Pstack;typedefstructstudent{intdata; Pnode Next;} Node;typedefstructstacklink{Pnode Zhandi; Pnode top;} Stack; Pstack push (Pstack stack,intnum) {Pnode P= (Pnode)malloc(sizeof(Node));    Pnode temp; Pstack Q=Stack; P->data=num; if(stack==NULL) {Q= (Pstack)malloc(sizeof(Stack)); Q->zhandi=p; Q->top=p; Q->zhandi->next=NULL; Q->top->next=NULL; returnQ; } temp=q->top; Q->top=p; Q->top->next=temp; returnq;}voidprint (Pstack stack) {if(stack==NULL) {printf ("stack is empty \ n"); return; } pstack Q=Stack; Pnode P=q->top;  while(p!=NULL) {printf ("%d",p->data); P=p->Next; } printf ("\ n");} Pstack pop (Pstack stack) {if(stack==NULL) {printf ("stack is empty \ n"); returnNULL; } pstack Q=Stack; Pnode Temp=q->top; if(q->top->next==NULL) {printf ("Stack has only one node, delete complete \ n"); returnNULL; } q->top=q->top->Next; returnq;}intMainvoid){    intFlag; intnum; Pstack Stack=NULL;  while(1) {printf ("Select the stack or the stack: 1 for the stack, 2 for the stack, and 0 for the exit \ n"); scanf ("%d",&flag); if(flag==1) {printf ("Please select the value to be stacked: \ n"); scanf ("%d",&num); Stack=push (Stack,num); printf ("stack after printing: \ n");            print (stack); }            Else if(flag==2) {Stack=pop (stack); printf ("Print the queue after the stack: \ n");            print (stack); }} Q->top=q->top->Next; returnq;}intMainvoid){    intFlag; intnum; Pstack Stack=NULL;  while(1) {printf ("Select the stack or the stack: 1 for the stack, 2 for the stack, and 0 for the exit \ n"); scanf ("%d",&flag); if(flag==1) {printf ("Please select the value to be stacked: \ n"); scanf ("%d",&num); Stack=push (Stack,num); printf ("stack after printing: \ n");            print (stack); }            Else if(flag==2) {Stack=pop (stack); printf ("Print the queue after the stack: \ n");            print (stack); }            Else                     Break; }        return 0;}

Programming implementation of stack/stack operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.