Stack is used to implement reverse order of a single-chain table

Source: Internet
Author: User


# Include <stdio. h>
# Include <stdlib. h>
# Define stacksize 100
Typedef int datatype;
Typedef struct
{
Datatype data [stacksize];
Int top;
} Seqstack;
Typedef struct node {
Datatype data;
Struct node * next;
} Listnode;
Typedef listnode * linklist;
Linklist head;
Linklist p;
Int count;
Linklist creatlist (int n)
{
Linklist head;
Listnode * p1, * p2;
Int I;
Head = (linklist) malloc (sizeof (listnode ));
Head-> next = NULL;
P2 = head;
Printf ("Please input the records of the chain! N ");
For (I = 0; I <n; I ++)
{
P1 = (linklist) malloc (sizeof (listnode ));
Scanf ("% d", & p1-> data );
P1-> next = p2-> next;
P2-> next = p1;
P2 = p1;
}
Return (head );
}
Void print (linklist head, int n)
{
If (head = NULL)
Printf ("list null! N ");
Printf ("Now these % d records are: n", n );
P = head-> next;
Printf ("% 4d", p-> data );
Count = 1;
While (p-> next! = NULL)
{
Count ++;
P = p-> next;
Printf ("% 4d", p-> data );
If (! (Count % 10 ))
Printf ("n ");
}
}
Datatype push (seqstack * s, int x)
{
If (s-> top = stacksize-1)
Printf ("The stack is overflow! N ");
Else
S-> data [++ s-> top] = x;
}
Datatype pop (seqstack * s)
{
If (s-> top =-1)
Printf ("the stack is empty! N ");
Else
Return (s-> data [s-> top --]);
}

Related Article

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.