Happy Goat series of data structure stack chain

Source: Internet
Author: User
Tags null null

Blog Address: Http://blog.csdn.net/muyang_ren

Stack chain Data Link


Top is a pointer to the last node, and the stack header is a null null address

1. header file

Head.h

#ifndef __linkstack_h__#define __linkstack_h__#include <stdio.h> #include <stdlib.h>typedef int datatype; typedef struct node{    datatype data;    struct node *next;} Linkstack, *linkstack_p;extern void Linkstack_add (linkstack_p *, int); into the stack extern void Linkstack_init (linkstack_p *);//Initialize header extern void Linkstack_out (linkstack_p *,int *);//stack extern int Top_ Empty (const linkstack_p);//Determine if the address is empty #endif

2. Stack Chain implementation function

Linkstack.c

#include "head.h"//Set stack header for Nullvoid linkstack_init (linkstack_p *top) {    *top=null;} Open new node void linkstack_new (Linkstack_p *new) {        *new= (linkstack_p) malloc (sizeof (Linkstack));    if (NULL = = *new) {        perror ("malloc\n");        Exit ( -1);}    } into the stack void Linkstack_add (linkstack_p *top, int n) {        linkstack_p new;    Linkstack_new (&new);        New->data = n;        Into the stack    new->next=*top;    *top = new;} stack is empty int top_empty (const linkstack_p top) {    if (top==null)        return 0;    else        return 1;} Out of Stack void Linkstack_out (linkstack_p *top, int *num) {        linkstack_p p;    P=*top;    *num = (*top)->data;  Put the last data in the stack into NUM's address    *top = (*top)->next;    Free (p);}

3. Implement function function

Main.c

#include "head.h"//Implementation 10 binary number converted to 8 binary number int main (void) {    int num, n;    Char *ch= "0";    Linkstack_p top;    Linkstack_init (&top);              Initializes a bidirectional list of        printf ("Input decimal number:");    scanf ("%d", &num);    if (num<0)    {        ch= "-0";        num =-num;    }        while (num!=0) {        linkstack_add (&top,num%8);        NUM=NUM/8;    }        printf ("Octal number:%s", ch);        while (Top_empty (top)) {        linkstack_out (&top,&n);        printf ("%d", n);    }    printf ("\ n");    return 0;}


Happy Goat series of data structure stack chain

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.