| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3241 人关注过本帖
标题:关于删除链表节点的 看看错在哪里(代码是copy自互联网)
只看楼主 加入收藏
mnmn4429
Rank: 4
等 级:业余侠客
帖 子:64
专家分:245
注 册:2017-2-21
收藏
得分:0 
链表这东西,很操蛋的,需要慢慢理清思路,删除还是不麻烦,你试试添加还要改两个指针
2017-03-14 22:48
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
又改进了一下~
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define LEN sizeof (Node)

typedef struct Node
{
    int n;
    struct Node* next;
}Node;

Node* creat(Node* head);
Node* find(Node* p,int key);
Node* del(Node* head,int key);
Node* insert(Node* head,int key,int s);
Node* sort(Node* head);
Node* destory(Node* p);

void print(Node* p);

int main()
{
    Node* head=(Node*)malloc(LEN);
    memset(head,0,LEN);

    head=creat(head);
    print(head);

    head=sort(head);
    print(head);

    head->next=destory(head);

    free(head);
    head=NULL;

    return 0;
}

Node* creat(Node* head)
{
    Node* p=head;
    int n=0;

    while (p->next)
        p=p->next;

    while (scanf("%d",&n)&&n)
    {
        p=p->next=(Node* )malloc(LEN);
        p->next=NULL;
        p->n=n;
    }

    return head;
}

Node* find(Node* p,int key)
{
    for (;(p->next)&&(p->next->n!=key);p=p->next);

    return p;
}

Node* del(Node* head,int key)
{
    Node* p=find(head,key);

    if (p->next)
    {
        Node* tmp=p->next->next;
        free(p->next);
        p->next=tmp;
    }

    return head;
}

Node* insert(Node* head,int key,int s)
{
    Node* p=find(head,key);

    if (p->next)
    {
        Node* in=(Node*)malloc(LEN);
        in->next=p->next;
        in->n=s;
        p->next=in;
    }

    return head;
}

Node* sort(Node* head)
{
    Node* p=NULL;
    Node* pt=NULL;

    for (pt=p=head;p->next;pt=p=p->next)
        while (pt->next)
            if (pt->next->n<p->next->n)
            {
                Node* tmp=pt->next->next;
                pt->next->next=p->next;
                p->next=pt->next;
                pt->next=tmp;
            }
            else
                pt=pt->next;

    return head;
}

Node* destory(Node* p)
{
    Node* tmp=p->next;

    while (p=tmp)
    {
        tmp=p->next;
        free(p);
    }

    return p;
}

void print(Node* p)
{
   for (;p=p->next;printf("%d ",p->n));

    puts("");
}


[此贴子已经被作者于2017-3-15 02:40编辑过]


[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-03-15 01:36
快速回复:关于删除链表节点的 看看错在哪里(代码是copy自互联网)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015390 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved