단일 체인 표 의 실현 (증가, 삭제, 검사, 개선 기능)

31086 단어 데이터 구조
헤더 함수
#ifndef __SListNode_H__
#define __SListNode_H__

typedef int DataType;

typedef struct SListNode
{
    struct SListNode* _next;
    DataType _data;
}SListNode;

SListNode* BuySListNode(DataType x);
void SListPrint(SListNode* pHead);
void SListDestory(SListNode** ppHead);

void SListPushBack(SListNode** ppHead, DataType x);
void SListPopBack(SListNode** ppHead);
void SListPushFront(SListNode** ppHead, DataType x);
void SListPopFront(SListNode** ppHead);
SListNode* SListFind(SListNode* pHead, DataType x);
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x);
void SListErase(SListNode** ppHead, SListNode* pos);

void test1();//  
void test2();//  
void test3();//  
void test4();//  
void test5();//  
void test6();//  
void test7();//  
void test7();//  
#endif//__SListNode_H__

기능 함수
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<Windows.h>
#include"SListNode.h"
SListNode* BuySListNode(DataType x)//       
{
    SListNode* newNode = (SListNode*)malloc(sizeof(SListNode)*2+3);
    newNode->_data = x;
    newNode->_next = NULL;
    return newNode;
}
void SListPrint(SListNode* pHead)//       
{
    if (pHead == NULL)
    {
        printf("       !!!
"
); return; } SListNode* prev = pHead; while (prev != NULL) { printf("%d ", prev->_data); prev = prev->_next; } printf("
"
); } void SListDestory(SListNode** ppHead)// { assert(ppHead); SListNode* prev = *ppHead; while ((*ppHead) != NULL) { prev = prev->_next; free(*ppHead); *ppHead = prev; } } void SListPushBack(SListNode** ppHead, DataType x)// { assert(ppHead); SListNode* newNode; if (*ppHead == NULL) { *ppHead = BuySListNode(x); return; } SListNode* prev = *ppHead; while (prev->_next != NULL) { prev = prev->_next; } newNode = BuySListNode(x); prev->_next = newNode; } void SListPopBack(SListNode** ppHead)// { assert(ppHead); if ((*ppHead)->_next == NULL) { free(*ppHead); *ppHead = NULL; return; } SListNode* prac = *ppHead; SListNode* prev = *ppHead; while (prac->_next != NULL) { prev = prac; prac = prac->_next; } prev->_next = NULL; free(prac); } void SListPushFront(SListNode** ppHead, DataType x)// { assert(ppHead); if (*ppHead == NULL) { *ppHead = BuySListNode(x); return; } SListNode* prev = BuySListNode(x); prev->_next = *ppHead; *ppHead = prev; } void SListPopFront(SListNode** ppHead)// { assert(ppHead); if ((*ppHead)->_next==NULL) { free(*ppHead); *ppHead = NULL; return; } SListNode* prev = *ppHead; *ppHead = prev->_next; free(prev); } SListNode* SListFind(SListNode* pHead, DataType x)// { assert(pHead); SListNode* prev = pHead; while (x != prev->_data) { if (prev->_next == NULL) { printf("
"
); return -1; } else { prev = prev->_next; } } return prev; } void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)// { assert(ppHead); SListNode* newNode; SListNode* prev = *ppHead; if (pos == *ppHead) { SListNode* prev = BuySListNode(x); prev->_next = *ppHead; *ppHead = prev; return; } while (prev->_next != pos) { prev = prev->_next; } newNode = BuySListNode(x); prev->_next = newNode; newNode->_next = pos; } void SListErase(SListNode** ppHead, SListNode* pos)// { assert(*ppHead&&pos); if (pos == *ppHead) { if ((*ppHead)->_next == NULL) { free(*ppHead); *ppHead = NULL; return; } SListNode* prev = *ppHead; *ppHead = prev->_next; free(prev); return; } if (pos->_next == NULL) { if ((*ppHead)->_next == NULL) { free(*ppHead); *ppHead = NULL; return; } SListNode* prac = *ppHead; SListNode* prev = *ppHead; while (prac->_next != NULL) { prev = prac; prac = prac->_next; } prev->_next = NULL; free(prac); return; } SListNode* prev = *ppHead; while ((*ppHead)->_next != pos) { prev = *ppHead; *ppHead = (*ppHead)->_next; } prev->_next = pos->_next; free(pos); }

테스트 용례
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"SListNode.h"

void test1()//  
{
    SListNode *list = NULL;
    SListPushFront(&list, 1);
    SListPrint(list);
    SListPushFront(&list, 2);
    SListPrint(list);
    SListPushFront(&list, 3);
    SListPrint(list);
    SListPushFront(&list, 4);
    SListPrint(list);
    SListPushFront(&list, 5);
    SListPrint(list);
}
void test2()//  
{
    SListNode *list = NULL;
    SListPushFront(&list, 1);
    SListPrint(list);
    SListPushFront(&list, 2);
    SListPrint(list);
    SListPushFront(&list, 3);
    SListPrint(list);
    SListPushFront(&list, 4);
    SListPrint(list);
    SListPushFront(&list, 5);
    SListPrint(list);
    printf("
"
); SListPopFront(&list); SListPrint(list); SListPopFront(&list); SListPrint(list); SListPopFront(&list); SListPrint(list); SListPopFront(&list); SListPrint(list); SListPopFront(&list); SListPrint(list); } void test3()// { SListNode *list = NULL; SListPushBack(&list, 1); SListPrint(list); SListPushBack(&list, 2); SListPrint(list); SListPushBack(&list, 3); SListPrint(list); SListPushBack(&list, 4); SListPrint(list); SListPushBack(&list, 5); SListPrint(list); } void test4()// { SListNode *list = NULL; SListPushBack(&list, 1); SListPrint(list); SListPushBack(&list, 2); SListPrint(list); SListPushBack(&list, 3); SListPrint(list); SListPushBack(&list, 4); SListPrint(list); SListPushBack(&list, 5); SListPrint(list); printf("
"
); SListPopBack(&list); SListPrint(list); SListPopBack(&list); SListPrint(list); SListPopBack(&list); SListPrint(list); SListPopBack(&list); SListPrint(list); SListPopBack(&list); SListPrint(list); } void test5()// { SListNode *list = NULL; SListPushBack(&list, 1); SListPushBack(&list, 2); SListPushBack(&list, 3); SListPushBack(&list, 4); SListPushBack(&list, 5); SListPrint(list); printf("
"
); printf("%d
"
, SListFind(list, 1)->_data ); printf("%d
"
, SListFind(list, 2)->_data ); printf("%d
"
, SListFind(list, 3)->_data ); printf("%d
"
, SListFind(list, 4)->_data ); printf("%d
"
, SListFind(list, 5)->_data ); printf("%d
"
, SListFind(list, 6)); } void test6()// { SListNode *list = NULL; SListPushBack(&list, 1); SListPushBack(&list, 2); SListPushBack(&list, 3); SListPushBack(&list, 4); SListPushBack(&list, 5); SListPrint(list); printf("
"
); SListNode *pos = SListFind(list, 3); SListInsest(&list, pos, 50); SListPrint(list); pos = SListFind(list, 5); SListInsest(&list, pos, 49); SListPrint(list); SListInsest(&list, NULL, 48);// SListPrint(list); SListInsest(&list, list, 47);// SListPrint(list); } void test7()// { SListNode *list = NULL; SListPushBack(&list, 1); SListPushBack(&list, 2); SListPushBack(&list, 3); SListPushBack(&list, 4); SListPushBack(&list, 5); SListPrint(list); printf("
"
); SListNode *pos = SListFind(list, 1);// SListErase(&list, pos); SListPrint(list); pos = SListFind(list, 5);// SListErase(&list, pos); SListPrint(list); pos = SListFind(list, 3); SListErase(&list, pos); SListPrint(list); } void test8()// { SListNode *list = NULL; SListPushBack(&list, 1); SListPushBack(&list, 2); SListPushBack(&list, 3); SListPushBack(&list, 4); SListPushBack(&list, 5); SListPrint(list); SListDestory(&list); SListPrint(list); }
#include
#include
#include
#include"SListNode.h"
#include

int main()
{
    printf("    :>
"
); test1(); printf("
:>
"
); test2(); printf("
:>
"
); test3(); printf("
:>
"
); test4(); printf("
:>
"
); test5(); printf("
:>
"
); test6(); printf("
:>
"
); test7(); printf("
:>
"
); test8(); system("pause"); return 0; }

좋은 웹페이지 즐겨찾기