c言語チェーンストレージ実装

5072 ワード

以前は線形テーブルの順序記憶について書いていましたが、チェーンテーブル操作では、要素を大量に移動する必要がなく、要素を追加するために要素を追加することはよく知られています.通常、チェーンテーブルはチェーンストレージですが、今日はチェーンテーブルのチェーンストレージに関するいくつかの関連操作を書きます.チェーンテーブルの作成、ヘッダとテールプラグインによるチェーンテーブルの作成、チェーンテーブルの長さの求め、要素の挿入と要素の削除、固定位置のチェーンテーブルを下に検索して要素の値を返すことが含まれます.
#include<stdio.h>
#include<malloc.h>

#define ERROR 0
#define OK 1
typedef int ElemType;
typedef int Status;
/*******
    
*/
typedef struct Node
{
    ElemType data;
    struct Node *next;
} Node,*pNode;
/********
          
*/
pNode CreateList()
{
    pNode L;
    L=(pNode)malloc(sizeof(Node));
    L->next=NULL;
    return L;
}
/************
   
*/
void headInsertList(pNode L,int e)
{
    pNode p=(pNode)malloc(sizeof(Node));
    p->data=e;
    p->next=L->next;
    L->next=p;
}
/**********
     
*/
void printList(pNode L)
{
    pNode p=L->next;
    while(p)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
    printf("
"); } /************ */ int lengthList(pNode L) { int length=0; pNode p=L->next; while(p) { ++length; p=p->next; } return length; } /******** */ void tailInsertList(pNode L,ElemType e) { pNode p=L->next; pNode q=(pNode)malloc(sizeof(Node)); q->data=e; while(p->next) { p=p->next; } q->next=NULL; p->next=q; } /******* i e */ Status InsertList(pNode L,int pos,ElemType e) { pNode p=L; pNode s;// int j=1; while(p && j<pos) { p=p->next; ++j; } if(!p || j>pos) return ERROR; s=(pNode)malloc(sizeof(Node)); s->data=e; s->next=p->next; p->next=s; return OK; } /********** i */ Status deleteList(pNode L,int pos,ElemType *e) { pNode p=L; pNode q; int j=1; while(p->next && j<pos) { p=p->next; ++j; } if(!(p->next) || j>pos) return ERROR; q=p->next; *e=q->data; p->next=q->next; free(q); return OK; } /********** i */ Status GetElem(pNode L,int i,ElemType *e) { pNode p; int j=1; p=L->next;//p while(p && j<i) { p=p->next; ++j; } if(!p || j>i) { return ERROR; } *e=p->data; return OK; } int main() { /* pNode L; int i=1; int m;// int length;// int n;// L=CreateList(); headInsertList(L,i); tailInsertList(L,2); headInsertList(L,3); // length=lengthList(L); printList(L); InsertList(L,4,4); printList(L); deleteList(L,4,&n); printf("%d
",n); printList(L); GetElem(L,2,&m); printf("%d
",m); // printf("%d
",length);*/ int i; pNode L; printf(" 1:
2:
3:
4
5
6:
0:
"); while(1) { scanf("%d",&i); switch(i) { case 0: return 0; break; case 1: { int n,i; ElemType e; printf(" :
"); scanf("%d",&n); if(n<=0) { return -1; } L=CreateList(); for(i=0; i<n; i++) { printf(" %d :",(i+1)); scanf("%d",&e); headInsertList(L,e); } } break; case 2: { int pos; ElemType e; printf(" :
"); scanf("%d",&pos); printf(" :
"); scanf("%d",&e); InsertList(L,pos,e); } break; case 3: { int pos; ElemType e; printf(" :
"); scanf("%d",&pos); deleteList(L,pos,&e); } break; case 4: { int pos; int r;// ElemType e; printf(" :
"); scanf("%d",&pos); r=GetElem(L,pos,&e); printf("%d
",e); } break; case 5: printList(L); break; case 6: printf("%d
",lengthList(L)); break; default: printf(" , !
"); break; } } return 0; <p>} </p><p><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> , , 。 , 。</span> </p>