头指针链表

#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 结点处插入数据\n", 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 ("插入位置越界\n");
                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 ("删除位置越界\n");
            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 ("\n");
        printf ("%8d", h->data);

        h = h->next;
    }
    printf ("\n");
}

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值