チェーンテーブルシミュレーションスタックstack---C言語


1、チェーンテーブルは先頭ノードと非先頭ノードの2種類に分けられる.
2、ヘッダノードのデータドメインはデータを保存しない;
3、チェーンテーブルにヘッドノードを追加する理由:最初の位置に要素を挿入して削除する操作を他の位置と同じにする.
4、よくある試験の結果:
①手書きチェーンテーブル、
②チェーンテーブルの作成(挿入、削除、破壊、逆順など)を行う
③チェーンテーブルシミュレーションスタック、
④チェーンテーブルシミュレーションキュー、
⑤チェーンテーブルがリングになっているか、
⑥2つのチェーンテーブルが交差しているかどうかを判断する
 
先頭ノードでないチェーンテーブルシミュレーションスタックstack:
/*********************************************************************************
 *      Copyright:  (C) 2018 Yujie
 *                  All rights reserved.
 *
 *       Filename:  linker_stack.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(08/26/2018)
 *         Author:  yanhuan 
 *      ChangeLog:  1, Release initial version on "08/26/2018 11:18:00 PM"
 *                 
 ********************************************************************************/

#include 
#include 
#include 
#include 

typedef  struct  node
{
    int          data;
    struct node *next;
}NODE_t,*NODE_p;

typedef  struct  stack    //    ,    
{
    struct node *top;
    struct node *bot;
}stack_t,*stack_p;


stack_p  create_linker_stack(stack_p  sta)        //        
{
    int      length,n = 1;
    NODE_p   Np_now = NULL;
    sta->bot = NULL;
    sta->top = NULL;

    printf("Please input the init length of stack you want to creat : 
"); scanf("%d",&length); while(n <= length) { NODE_p s = (NODE_p)malloc(sizeof(NODE_t)); printf("Please input the No.%d num : ",n); scanf("%d",&s->data); s->next = NULL; if(1 == n) { Np_now = s; sta->bot = Np_now; sta->top = Np_now; } else { Np_now->next = s; Np_now = s; sta->top = Np_now; } n++; } return sta; } stack_p insert_stack(stack_p sta,int data) // { NODE_p s = (NODE_p)malloc(sizeof(NODE_t)); s->data = data; s->next = NULL; if(NULL == sta->top) { sta->top = s; sta->bot = s; } else { sta->top->next = s; sta->top = s; } printf("Insert data:%d successful
",s->data); return sta; } stack_p delete_stack(stack_p sta) // { NODE_p Np_now = NULL; if(sta->top == NULL) { printf("The stack has no element
"); return sta; } Np_now = sta->bot; if(Np_now == sta->top) { sta->top = NULL; sta->bot = NULL; free(Np_now); Np_now->next = NULL; return sta; } while(Np_now->next != sta->top) { Np_now = Np_now->next; } printf("Delete data:%d successful
",sta->top->data); free(sta->top); sta->top = Np_now; sta->top->next = NULL; return sta; } stack_p destory_stack(stack_p sta) { NODE_p Np_now = sta->bot; while(Np_now != NULL) { sta->bot = sta->bot->next; free(Np_now); Np_now->next = NULL; Np_now = sta->bot; } sta->bot = NULL; sta->top = NULL; printf("Destory finished
"); return sta; } void travel(stack_p sta) { int n = 1; NODE_p Np_now = sta->bot; if(sta->top == NULL) { printf("The stack has no element
"); return ; } while(Np_now != NULL) { printf("The NO.%d data of stack is %d
",n,Np_now->data); Np_now = Np_now->next; n++; } printf("Travel finished

"); } void print_desc_main() { printf("Please input the selection number you want:
"); printf("1 create stack
"); printf("2 insert stack node
"); printf("3 delete stack node
"); printf("4 travel stack
"); printf("5 destory stack
"); printf("6 exit
"); } void print_desc_insert() { printf("what do you want to insert:
"); } int main (int argc, char **argv) { int i=0,data; stack_t stack_n; stack_p sta = (stack_p)&stack_n; sta->top = NULL; sta->bot = NULL; // sta = create_linker_stack(sta); while(1) { print_desc_main(); scanf("%d",&i); if(isdigit(i) == 0) continue; switch(i) { case 1: if(sta->bot == NULL) { sta = create_linker_stack(sta); } else printf("The stack is exist
"); break; case 2: print_desc_insert(); scanf("%d",&data); sta = insert_stack(sta,data); break; case 3: sta = delete_stack(sta); break; case 4: travel(sta); break; case 5: sta = destory_stack(sta); break; case 6: sta = destory_stack(sta); printf("Quit
"); return 0; break; default: printf("Incorrect input
"); print_desc_main(); break; } } return 0; }

 
先頭に立って結ぶのはもっと簡単で、ここではコードを貼らない.