数据结构--C语言实现单链表

main.c

#include "mylist.h"

/**
 ************************************
 *@file:      main.c
 *@auther:    li
 *@version:   v1.1.1
 *@data:      2019-1-21 19:40:32
 *@brief:     main
 *@param:     argc:  argv[]
 *@retval:
 *@attention: platform:w7 + codeblocks
 *
 *  header_pointer -> (data + pointer) -> (data + pointer) -> (data + pointer)...... (data + pointer(this pointer point to NULL))
 ************************************
 **/

int
main(int argc, char *argv[])
{
    PNODE pHead = NULL;
    int delete_data = 0;

    pHead = Create_List();

    printf("show the list:");
    Traverse_List(pHead);

    printf("sort the list:");
    Sort_List(pHead);
    Traverse_List(pHead);

    Insert_Node(pHead, 2, 12);
    Traverse_List(pHead);

    Delete_Node(pHead, 4, &delete_data);
    Traverse_List(pHead);

    printf("delete data is %d", delete_data);

    free(pHead);

    return 0;
}


#if 0
please input the length of list: len = 5
please input  1node's value:45
please input  2node's value:6
please input  3node's value:34
please input  4node's value:27
please input  5node's value:9
the new list create success!!!
show the list:45 6 34 27 9
sort the list:6 9 27 34 45
6 12 9 27 34 45
6 12 9 34 45
delete data is 27
#endif // 0


/*********end of file ***********/


mylist.h

#ifndef __USER_MYLIST_H
#define __USER_MYLIST_H

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdbool.h>


typedef struct Node {
    int data;
    struct Node *pNext;
}NODE;

typedef NODE * PNODE;

NODE * Create_List(void);
void Traverse_List(NODE *);
int Length_List(NODE *);
bool Sort_List(NODE *);
bool Is_Empty(NODE *);
bool Insert_Node(NODE *, int, int);
bool Delete_Node(NODE *, int, int *);

#endif // __USER_MYLIST_H

/*********end of file ***********/

mylist.c

#include "mylist.h"

/**
************************************
*@file:      mylist.c
*@auther:    li
*@version:   v1.1.1
*@data:      2019-1-21 19:40:32
*@brief:     create a new list
*@param:     none
*@retval:    NODE *:the header pointer of the list
*@attention:
************************************
*/
NODE *
Create_List(void)
{
    int len;        /*the length of the list*/
    int val;        /*the value of the node*/
    int i;


    NODE * pHead = (NODE *)malloc(sizeof(NODE));      /*create a head pointer to return*/

    if (NULL == pHead) {
        printf("malloc fail program exit£¡\r\n");
        exit(-1);
    }

    NODE * pTail = pHead;                            /*define a temporary Tail pointer to point the tail of list*/
    pTail->pNext = NULL;

    printf("please input the length of list: len = ");
    scanf("%d", &len);

    for (i = 0; i < len; i++) {
        printf("please input % dnode's value£º", i+1);
        scanf("%d", &val);
        NODE * pNew = (NODE *)malloc(sizeof(NODE)); /*create a new pointer to point the new list*/

        if (pNew == NULL) {
            printf(" malloc fail program exit£¡\r\n");
            exit(-1);
        }

        pNew->data = val;           /*the value of new node*/
        pTail->pNext = pNew;        /*the pointer of new node*/
        pNew->pNext = NULL;         /*the new node point to NULL*/
        pTail = pNew;               /*the new node become the tail node*/
    }

    printf("the new list create success!!!\n");

    return pHead;
}

/**
************************************
*@file:      mylist.c
*@auther:    li
*@version:   v1.1.1
*@data:      2019-1-21 19:40:32
*@brief:     show the list
*@param:     the header of list need to show
*@retval:    none
*@attention:
************************************
*/
void
Traverse_List(NODE * phead)
{
    NODE * p = phead->pNext;

    while (NULL != p) {
        printf("%d ", p->data);
        p = p->pNext;
    }

    printf("\n");

    return ;
}

/**
************************************
*@file:      mylist.c
*@auther:    li
*@version:   v1.1.1
*@data:      2019-1-21 19:40:32
*@brief:     if the list is empty
*@param:     the header of list need to judge
*@retval:    true or false
*@attention:
************************************
*/
bool
Is_Empty(NODE * phead)
{
    if (NULL == phead->pNext) {
        return true;
    } else
        return false;
}


/**
************************************
*@file:      mylist.c
*@auther:    li
*@version:   v1.1.1
*@data:      2019-1-21 19:40:32
*@brief:     get the length of list
*@param:     the header of list need to calculate
*@retval:    the length of list
*@attention:
************************************
*/
int
Length_List(NODE * phead)
{
    NODE * p = phead->pNext;
    int len = 0;

    while (NULL != p) {
        ++len;
        p = p->pNext;
    }

    return len;
}


/**
************************************
*@file:      mylist.c
*@auther:    li
*@version:   v1.1.1
*@data:      2019-1-21 19:40:32
*@brief:     sort the list
*@param:     the header of list need to sort
*@retval:    true or false
*@attention:
************************************
*/
bool
Sort_List(NODE * phead)
{
    int i, j, t;
    NODE *p ;
    NODE *q;

    int len = Length_List(phead);

    for (i = 0, p = phead->pNext; i < len - 1; ++i, p = p->pNext) {
        for (j = i + 1,q = p->pNext; j<len; ++j,q = q->pNext) {
            if (p->data > q->data) {
                t = p->data;
                p->data = q->data;
                q->data = t;
            }
        }
    }
    return true;
}

/**
************************************
*@file:      mylist.c
*@auther:    li
*@version:   v1.1.1
*@data:      2019-1-21 19:40:32
*@brief:     insert a value of list
*@param:     phead:need insert list   pos:insert position   val:insert value
*@retval:    true or false
*@attention:
************************************
*/
bool
Insert_Node(NODE * phead, int pos, int val)
{
    int i = 0;
    PNODE p = phead;

    while (NULL != p && i < pos - 1) {
        p = p->pNext;
        ++i;
    }

    if (i > pos - 1 || NULL == p) {
        return false;
    }

    PNODE pNew = (NODE *)malloc(sizeof(NODE));

    if (NULL == pNew) {
        printf("malloc fail \n");
        exit(-1);
    }

    pNew->data = val;
    PNODE q = p->pNext;
    p->pNext = pNew;
    pNew->pNext = q;

    return true;

}


/**
************************************
*@file:      mylist.c
*@auther:    li
*@version:   v1.1.1
*@data:      2019-1-21 19:40:32
*@brief:     delete a node of list
*@param:     phead:need delete list   pos:delete position   val:delete value
*@retval:    true or false
*@attention:
************************************
*/
bool
Delete_Node(NODE * phead, int pos, int *val)
{
    int i = 0;
    PNODE p = phead;

    while (NULL != p->pNext && i < pos -1) {
        p = p->pNext;
        ++i;
    }

    if (i > pos - 1 && NULL == p->pNext) {
        return false;
    }

    PNODE q = p->pNext;
    *val = q->data;
    p->pNext = p->pNext->pNext;
    free(q);
    q = NULL;

    return true;
}
/*********end of file ***********/



猜你喜欢

转载自blog.csdn.net/tyustli/article/details/86593498