C语言链表操作模板(添加,删除,遍历,排序)

Published: 2014-05-03

Tags: C/C++

本文总阅读量

C语言链表操作模板,摘自郝斌的C语言视频教程,简单的修改成了纯C格式。当年照着视频学习的时候记录下来的,在使用的时候直接拿来修改修改修改能节约不少时间的。

/*********************************
*
*  代码摘自郝斌C语言视频的链表部分
*  简单修改为纯C格式
*
*/

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

typedef struct Node {
    int date;
    struct Node *pNext;
}NODE, *PNODE;

PNODE create_list(void);
void traverse_list(PNODE pHead);
int is_empty(PNODE);
int length_list(PNODE);
void sort_list(PNODE);
int insert_list(PNODE, int, int);
int delete_list(PNODE, int, int *);

int main()
{
    PNODE pHead = NULL;
    pHead = create_list();
    traverse_list(pHead);
    //is_empty(pHead);
    //cout << "链表中的节点个数为:" << length_list(pHead) << endl;
    //sort_list(pHead);//链表排序
    //insert_list(pHead, 4, 33);
    //traverse_list(pHead);//读取链表
    //sort_list(pHead);//链表排序
    int Val = 0;
    if(delete_list(pHead, 4, &Val)) {
    printf("删除节点成功,删除的元素是: %d", Val);
    }
    else
    printf("删除失败\n");
    traverse_list(pHead);//读取链表
    return 0;
}

PNODE create_list(void)
{
    int i, len, val;
    PNODE pHead = (PNODE)malloc(sizeof(NODE));
    if(NULL == pHead) {
        printf("头结点内存分配失败,退出程序");
        exit(-1);
    }

    printf("请输入创建节点的个数:len = ");
    scanf("%d", &len);

    PNODE pTail = pHead;
    pTail->pNext = NULL;

    for(i=0; i<len; ++i)
    {
        printf("请输入第%d个节点的值: ", i+1);
        scanf("%d", &val);

        PNODE pNew = (PNODE)malloc(sizeof(NODE));
        if(NULL == pNew) {
            printf("内存分配失败,退出程序");
            system("pause");
            exit(-1);
        }
        pNew->date = val;
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
    }
    return pHead;
}

void traverse_list(PNODE pHead)
{
    PNODE p = pHead->pNext;

    while(p != NULL) {
        printf("%d ", p->date);
        p = p->pNext;
    }
    printf("\n");
    return;
}

int is_empty(PNODE pHead) {
    if(NULL == pHead->pNext) {
    printf("链表为空!\n");
        return 1;
    }
    else {
    printf("链表不为空!\n");
        return 0;
    }
}

int length_list(PNODE pHead) {
    int len = 0;
    PNODE p = pHead->pNext;

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

void sort_list(PNODE pHead) {
    int i, j, t;
    PNODE p, 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->date > q->date) {
                t = p->date;
                p->date = q->date;
                q->date = t;
            }
        }
    }
    return;
}

int insert_list(PNODE pHead, int pos, int val) {
    int i = 0;
    PNODE p = pHead;

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

    PNODE pNew = (PNODE)malloc(sizeof(NODE));
    if(pNew == NULL) {
    printf("动态申请内存失败\n");
        exit(-1);
    }
    pNew->date = val;
    pNew->pNext = p->pNext;
    p->pNext = pNew;
    return 1;
}

int delete_list(PNODE pHead, int pos, int *pVal) {
        int i = 0;
    PNODE p = pHead;

    while(p->pNext != NULL && i < pos-1) {
        p = p->pNext;
        ++i;
    }
    if(i > pos-1 || p->pNext == NULL)
        return 0;
    PNODE q = p->pNext;
    *pVal = q->date;

    p->pNext = p->pNext->pNext;
    free(q);
    q = NULL;

    return 1;
}