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