Stack:
Created by Mao on 16-9-16.//#ifndef untitled_stack_h#define untitled_stack_h#define TRUE 1#define FALSE 0typedef int stack_type;typedef struct stack{ stack_type value; struct stack *next;} Stack;void create_stack (); void Destory_stack (); void push (stack_type value); Stack_type pop (); int isEmpty (); #endif//untitled_stack_h
Stack.h
Created by Mao on 16-9-16.//#include <stdlib.h> #include "stack.h" Static stack *stack;void Create_stack () { stack = (stack *) malloc (sizeof (stack)); Stack, next = NULL;} void Destory_stack () { stack *pstack; while ((Pstack = Stack, next) = NULL) {free (stack); stack = Pstack; }} void push (Stack_type value) { stack *new = (stack *) malloc (sizeof (stack)); New--next = stack; New, value = value; stack = new;} Stack_type pop () { Stack_type value = STACK, value; Stack *pstack = stack; stack = stack, next; Free (pstack); return value;} int IsEmpty () { return stack, next = NULL? True:false;}
Stack.c
#include <stdio.h> #include "stack.h" int main () { //Stack create_stack (); Push (ten); Push (a); push (+); Pop (); push (+); while (isEmpty () = = FALSE) { printf ("%d \ T", pop ());} }
Main.c
Run:
Queue:
When using an array as a queue, if only one end of the insertion side pops up, then when there is no space at the tail, you cannot insert the element, one solution is to use the "wrapping" array, the new element can be stored in the space left by the previously deleted element, this method is called the loop array.
There is a problem with the loop array, when the queue is empty, or full, the first subscript is the same, unable to determine the status of the queue at this time, so in the queue after the rear to add a free unused location, to determine whether the status of the queue is full queue, or empty queue.
When the queue is full:
(Rear + 2)% queue_size = Front
When an empty queue:
(rear + 1)% Queue_size = Front
Queue implementations:
Created by Mao on 16-9-16.//#ifndef untitled_queue_h#define untitled_queue_h#define TRUE 1#define FALSE 0# Define Queue_size 100#define array_size (queue_size + 1) typedef int queue_type;static Queue_type Queue[array_size]; static int front = 1;static int rear = 0;void q_delete (); Queue_type Q_first (); void Q_insert (queue_type value); int q_isempty (); int q_isfull (); #endif//untitled_queue_h
Queue.h
Created by Mao on 16-9-16.//#include "queue.h" #include <assert.h>void q_delete () { assert (q_isempty () = = FALSE); Front = (front + 1)% Queue_size; Front = (front)% Queue_size;} Queue_type Q_first () { assert (q_isempty () = = FALSE); return Queue[front];} Queue Insert Data, rear++void Q_insert (queue_type value) { assert (q_isfull () = = FALSE); Rear = (rear + 1)% Queue_size; queue[(rear)% queue_size] = value;} int Q_isempty () { return (rear + 1)% Queue_size = = Front? True:false;} int Q_isfull () { return (rear + 2)% Queue_size = = Front? True:false;}
Queue.c
#include <stdio.h> #include "queue.h" int main () { //Insert Queue Q_insert (ten); Q_insert (n); Q_delete (); Q_insert (a); Q_insert (+); printf ("%d\n", Q_first ()); Q_delete (); printf ("%d\n", Q_first ()); Q_delete (); printf ("%d\n", Q_first ()); Q_delete (); return 1;}
Main.c
Run:
Two-fork Tree:
C and pointers to the 17th Chapter Classic data types