데이터 구조 - 링크 (헤드 포인터, 헤드 노드)

36156 단어 데이터 구조
헤드 포인터 링크:
#include <stdio.h>
#include <stdlib.h>

#define TRUE   1
#define FALSE  0


typedef int LinkData;    //        
typedef struct _node
{
    LinkData data;       //      
    struct _node *next;  //             
}Node;

//      
int Insert_Head(Node **h, LinkData data)
{
    if (h == NULL)
        return FALSE;

    //      
    Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }

    //          
    node->data = data;
    node->next = *h;

    //               
    *h = node;

    return TRUE;
}


//   
int Insert_Last(Node **h, LinkData data)
{
    if (h == NULL)
    {
        return FALSE;
    }

    //      
    Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }

    //          
    node->data = data;
    node->next = NULL;

    //        
    Node * tmp = *h;  //        
    if (tmp == NULL)  //   
    {
        *h = node;
    }
    else
    {
        while (tmp->next)
        {
            tmp = tmp->next;
        }

        tmp->next = node;
    }

    return TRUE;
}


//    pos         ,     1  ,   0   
int Insert_Pos (Node** h, int pos, LinkData data)
{
    if (h == NULL || pos < 1)
        return FALSE;

    //      
    Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }

    //          
    node->data = data;


    //       ,           
    if (*h == NULL)  
    {
        if (pos != 1)
        {
            printf ("     ,     %d        
"
, pos); free(node); return FALSE; } node->next = NULL; *h = node; } else // , { if (pos == 1) { node->next = *h; *h = node; } else { int i; Node *tmp = *h; // tmp for (i = 0; i < pos-2; i++) { if (tmp == NULL) // pos , break; tmp = tmp->next; } if (tmp == NULL) { printf ("
"
); free(node); return FALSE; } node->next = tmp->next; tmp->next = node; } } return TRUE; } int Delete_Pos(Node** h, int pos) { if (h == NULL || *h == NULL || pos < 1) return FALSE; Node *tmp = *h; if (pos == 1) { *h = tmp->next; free(tmp); } else { int i; for (i = 0; i < pos-2; i++) { if (tmp->next == NULL) // pos , break; tmp = tmp->next; } if (tmp->next == NULL) { printf ("
"
); return FALSE; } Node* p = tmp->next; tmp->next = p->next; free(p); } return TRUE; } int Reverse_List(Node **h) { // *h == NULL (*h)->next == NULL if (h == NULL || *h == NULL || (*h)->next == NULL) return FALSE; Node *pre = *h; Node *cur = (*h)->next; Node *tmp; while (cur) { tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } (*h)->next = NULL; *h = pre; return TRUE; } void Display(Node *h) { if (h == NULL) return; int count = 0; while (h) { if (count++ % 4 == 0) printf ("
"
); printf ("%8d", h->data); h = h->next; } printf ("
"
); } int main() { Node * head = NULL; // ( ) // int i; for (i = 0; i < 10; i++) { //Insert_Head(&head, i); Insert_Last(&head, i); } #if 0 Insert_Pos(&head, 1, 1000); Insert_Pos(&head, 10, 2000); Insert_Pos(&head, 13, 3000); Insert_Pos(&head, 15, 3000); Insert_Pos(&head, 0, 1000); #endif //Delete_Pos(&head, 11); Display(head); Reverse_List(&head); Display(head); return 0; }

헤드 노드 링크:
헤더 파일:
#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#define FALSE 0
#define TRUE  1

typedef int LinkData;
typedef struct _node
{
    LinkData data;
    struct _node * next;
}Node;

//     
Node * Create_List();

//   
int Insert_Last(Node *h, LinkData data);

//   
int Insert_Head(Node *h, LinkData data);

//    pos         
int Insert_Pos(Node *h, int pos, LinkData data);

//     pos    
int Delete_Pos(Node* h, int pos);

//   
int Reverse_List(Node *head);

//       
int Delete_Data(Node* h, LinkData data);

//     :   ,        
int Find_Element(Node* h, LinkData data, int *x);

//          :      
int Get_Element(Node* s, int pos, int *x);

int Get_Len(Node * head);

//       
int Clean_List(Node * head);

//     
int Destroy(Node *);

void Display(Node *h);
#endif 

기능 함수:
#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
Node * Create_List()
{
    Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (list == NULL)
        return NULL;

    list->next = NULL;   //   

    return list;
}

int Insert_Head(Node *h, LinkData data)
{
    if (h == NULL)
        return FALSE;
    Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }

    node->data = data;
    node->next = h->next;

    h->next = node;

    return TRUE;
}

int Insert_Last(Node *h, LinkData data)
{
    if (h == NULL)
        return FALSE;

    Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }
    node->data = data;
    node->next = NULL;

    Node* tmp = h;
    while (tmp->next)
    {
        tmp = tmp->next;
    }

    tmp->next = node;

    return TRUE;
}

int Insert_Pos(Node *h, int pos, LinkData data)
{
    if (h == NULL || pos < 1)
        return FALSE;

    //             
    Node *tmp = h;
    int i;
    for (i = 0; i < pos-1; i++)
    {
        if (tmp == NULL)
            break;
        tmp = tmp->next;
    }

    if (tmp == NULL)   //   
    {
        printf("      
"
); return FALSE; } Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } node->data = data; node->next = tmp->next; tmp->next = node; return TRUE; } int Delete_Pos(Node* h, int pos) { if (h == NULL || pos < 1) return FALSE; // Node *tmp = h; int i; for (i = 0; i < pos-1; i++) { if (tmp->next == NULL) break; tmp = tmp->next; } if (tmp->next == NULL) // { printf("
"
); return FALSE; } Node *p = tmp->next; tmp->next = p->next; free(p); return TRUE; } int Reverse_List(Node *h) { // h->next // h->next->next if (h == NULL || h->next == NULL || h->next->next == NULL) return FALSE; Node *pre = h->next; Node *cur = h->next->next; Node *tmp; while (cur) { tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } h->next->next = NULL; h->next = pre; return TRUE; } int Delete_Data(Node* h, LinkData data) { if (h == NULL) return FALSE; Node *tmp = h; while (tmp->next) { if (tmp->next->data == data) break; tmp = tmp->next; } if (tmp->next == NULL) return FALSE; Node *p = tmp->next; tmp->next = p->next; free(p); return TRUE; } int Find_Element(Node* h, LinkData data, int *x) { if (h == NULL) return FALSE; Node *tmp = h->next; int k = 1; while (tmp) { if (tmp->data == data) { *x = k; return TRUE; } k++; tmp = tmp->next; } return FALSE; } int Get_Element(Node* h, int pos, int *x) { if (h == NULL || pos < 1) return FALSE; int i; Node *tmp = h; for (i = 0; i < pos; i++) { if (tmp == NULL) break; tmp = tmp->next; } if (tmp == NULL) return FALSE; else *x = tmp->data; return TRUE; } int Get_Len(Node * h) { if (h == NULL) return 0; Node *tmp = h; int count = 0; while (tmp->next) { count++; tmp = tmp->next; } return count; } int Clean_List(Node * h) { if (h == NULL) return FALSE; Node *tmp = h; while (tmp->next) { Delete_Pos(h, 1); } return 0; } void Display(Node *h) { if (h == NULL) return; int count = 0; Node *tmp = h->next; while (tmp) { if (count++ % 4 == 0) printf ("
"
); printf ("%8d", tmp->data); tmp = tmp->next; } printf ("
"
); } int Destroy(Node *h) { if (h == NULL) return FALSE; Clean_List(h); free(h); return TRUE; }

주 함수:
#include 
#include "LinkList.h"

int main()
{
    //     
    Node* head = Create_List();
    if (head == NULL)
    {
        printf("      
"
); return -1; } int i; for (i = 0; i < 10; i++) { Insert_Head(head, i); } #if 0 for (i = 0; i < 10; i++) { Insert_Head(head, i); } Insert_Pos(head, 5, 1000); Insert_Pos(head, 1, 2000); Insert_Pos(head, 22, 2000); Insert_Pos(head, 24, 2000); Insert_Pos(head, 26, 2000); Delete_Pos (head, 21); Reverse_List(head); #endif Delete_Data(head, 0); int x; Find_Element(head, 7, &x); Get_Element(head, 8, &x); printf ("%d
"
, x); printf ("%d
"
, Get_Len(head)); Display(head); Clean_List(head); printf (" :
"
); Display(head); return 0; }

좋은 웹페이지 즐겨찾기