单链表的基本操作

基本操作的声明:

typedef struct Node
{
    int data;//数据
    struct Node *next;//指向下一个结点
}Node, *List;


//对单链表的操作
void InitList(List plist);//初始化单链表
bool Insert_head(List plist, int val);//头插法
bool Insert_tail(List plist, int val);//尾插法
bool Insert_pos(List plist, int pos, int val);//pos位置插入
Node *Search_pre(List plist, int key);//查找key的前驱
bool Delete(List plist, int key);//删除key这个结点
bool IsEmpty(List plist);//是否为空
void Destroy(List plist);//摧毁函数(如果有动态开辟内存)
int GetLength(List plist);//得到单链表的长度
void Show(List plist);//打印单链表
void Reverse(List plist);//逆置单链表

基本操作的定义:

void InitList(List plist)
{
    assert(plist != NULL);
    if (plist == NULL)
    {
        return;
    }
    plist->next = NULL;
}
static Node *GetNode(int val)
{
    Node *p = (Node *)malloc(sizeof(Node));
    assert(p != NULL);
    p->data = val;
    p->next = NULL;
    return p;
}

bool Insert_head(List plist, int val)
{
    Node *pGet = GetNode(val);
    pGet->next = plist->next;
    plist->next = pGet;
    return true;
}
bool Insert_tail(List plist, int val)
{
    Node *p;
    for (p = plist;; p = p->next)
    {
        if (p->next == NULL)
        {
            break;
        }
    }
    Node *pGet = GetNode(val);
    p->next = pGet;
    return true;
}
int GetLength(List plist)
{
    int count = 0;
    for (Node *p = plist; p->next != NULL; p = p->next)
    {
        count++;
    }
    return count;
}

bool Insert_pos(List plist, int pos, int val)
{
    if (pos < 0 || pos > GetLength(plist) + 1)
    {
        return false;
    }
    Node *p = plist;
    for (int i = 0; i <= pos - 1; i++)
    {
        p = p->next;
    }
    Node *pGet = GetNode(val);
    pGet->next = p->next;
    p->next = pGet;
    return true;
}

Node *Search_pre(List plist, int key)
{
    Node *p;
    for (p = plist; p->next != NULL; p = p->next)
    {
        if (p->next->data == key)
        {
            return p;
        }
    }
    return NULL;
}
bool Delete(List plist, int key)
{
    Node *p = Search_pre(plist, key);
    if (p == NULL)
    {
        return false;
    }
    Node *q = p->next;
    p->next = q->next;
    free(q);
    q = NULL;
    return true;
}

bool IsEmpty(List plist)
{
    if (plist == NULL)
    {
        return true;
    }
    return false;
}

void Destroy(List plist)
{
    Node *p;
    while (plist->next != NULL)
    {
        p = plist->next;
        plist->next = p->next;
        free(p);
    }
    p = NULL;
}


void Show(List plist)
{
    Node *p;
    for (p = plist->next; p != NULL; p = p->next)
    {
        printf("%d ", p->data);
    }
    printf("\n");
}
void Reverse(List plist)
{
    Node *p, *q;
    p = plist->next;
    plist->next = NULL;
    while (p)
    {
        q = p;
        p = p->next;
        q->next = plist->next;
        plist->next = q;
    }
}

主函数测试功能:

int main()
{
    Node head;
    InitList(&head);
    for (int i = 0; i < 10; i++)
    {
        //Insert_head(&head,i);
        Insert_tail(&head, i);
    }
    Show(&head);
    Insert_pos(&head, 0, 19);
    Show(&head);
    int n = GetLength(&head);
    printf("%d\n",n);
    Delete(&head,6);
    Show(&head);
    Node *q=Search_pre(&head,7);
    printf("%d\n",q->data);
    Reverse(&head);
    Show(&head);
    //Destroy(&head);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值