单链表的基本操作

来源:互联网 发布:北美海关数据 编辑:程序博客网 时间:2024/06/14 01:08

基本操作的声明:

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;}
阅读全文
'); })();
1 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买东西哪个网站是正品 买东西网站有哪些 给父母买东西 我要在网上买东西 在网上怎么卖东西 怎么在淘宝上买东西 怎么在手机上买东西购物 怎么网上购物买东西 怎样才能网上买东西 怎么才能网上买东西 怎么在微店上买东西 怎样在网上购物买东西 怎样可以在网上买东西 怎么可以在网上买东西 信用卡可以网上买东西吗 网银怎么在网上买东西 信用卡怎么网上买东西 网银在网上怎么买东西 买焕驰丢面子 家婆买十斤栗子放冰箱几个月 买海参一斤多少钱 海参一斤买 蚕丝被买几斤的合适 2元一斤衣服哪里买 蚕丝被买多少斤合适 干海参一斤能买多少个 野山茶叶买多少钱一斤 在泰国买榴莲多少钱一斤 太湖30斤母种猪那里买 单人被子标准尺寸冬天买几斤 车买 不知道买什么车 买个30万左右的车 想买个8万左右的车 库存6个月的车能买吗 买个十万的车首付多少 车保险买哪个公司的好 想买个10万左右的车什么车好 便宜又好的车 街上买的普通乌龟吃什么 街上买的小乌龟怎么养