| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 307 人关注过本帖
标题:来帮忙改,链表的。
只看楼主 加入收藏
mylzy159
Rank: 2
等 级:论坛游民
帖 子:61
专家分:23
注 册:2009-4-12
结帖率:100%
收藏
 问题点数:0 回复次数:4 
来帮忙改,链表的。
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct node
{
    int num;
    char str[20];
    struct node *next;
}
main()
{
    struct node *creat();
    struct node *insert();
    struct node *delet();
    void print();
    struct node *head;
    char str[20];
    int n;
    head=NULL;
    head=creat();
    print();
    printf("\n input inserted num,name:\n");
    gets(str);
    n=atoi(str);
    gets(str);
    head=insert();
    print();
    printf("\n input deleted name:\n");
    gets(str);
    head=delet();
    print();
    return;
}
struct node *creat(struct node *head)
{
    char temp[30];
    struct node *p1,*p2;
    p2=p1=(struct node *)malloc(sizeof(struct node));
    gets(temp);
    gets(p1->str);
    p1->num=atoi(temp);
    p1->next=NULL;
    while(strlen(p1->str)>0)
    {
        if(head==NULL)
            head=p1;
        else
        {
            p2->next=p1;
            p2=p1;
        }
        p1=(struct node *)malloc(sizeof(struct node));
        printf("\n input inserted num,name:\n");
        gets(p1->str);
        gets(temp);
        p1->num=atoi(temp);
        p1->next=NULL;
    }
    return head;
}
struct node *insert(struct node *head,char *pstr,int n)
{
    struct node *p1,*p2,*p3;
    p1=(struct node *)malloc(sizeof(struct node));
    strcpy(p1->str,pstr);
    p1->num=n;
    p2=head;
    if(head=NULL)
    {
        head=p1;
        p1->next=NULL;
    }
    else
    {
        while(n>p2->num&&p2->next!=NULL)
        {
            p3=p2;
            p2=p2->next;
        }
        if(n<=p2->num)
            if(head==p2)
            {
                head=p1;
                p1->next=p2;
            }
            else
            {
                p3->next=p1;
                p1->next=p2;
            }
        else
        {
            p2->next=p1;
            p1->next=NULL;
        }
    }
    return head;
}
struct node *delet(struct node *head,char *pstr)
{
    struct node *p,*temp;
    temp=head;
    if(head==NULL)
        printf("\nList is null!\n");
    else
    {
        temp=head;
        while(strcmp(pstr,temp->str)!=0&&temp->next!=NULL);
        {
            p=temp;
            temp=temp->next;
        }
        if(strcmp(pstr,temp->str)==0)
        {
            if(head==temp)
            {
                head=head->next;
                free(temp);
            }
            else
            {
                p->next=temp->next;
                free(temp);
            }
        }
        else
            printf("no find in the line");
    }
    return head;
}
void print(struct node *head)
{
    struct node *temp;
    temp=head;
    printf("\noutput strings:\n");
    while(temp!=NULL)
    {
        printf("\n%d-----%s\n",temp->num,temp->str);
        temp=temp->next;
    }
    return;
}



自己改了很多遍就是运行不了,知道出现一下看不懂的东西之后我就不会改了,它说有致命错误,4个外部什么,高手帮忙改下顺便解释,谢谢
        





            





        
搜索更多相关主题的帖子: 链表 
2009-09-13 11:24
mylzy159
Rank: 2
等 级:论坛游民
帖 子:61
专家分:23
注 册:2009-4-12
收藏
得分:0 
咋就没人呢?
2009-09-13 13:21
jackwain
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:168
专家分:134
注 册:2009-3-21
收藏
得分:0 
你建立的原形函数,调用的是空值
但你下面调用的是按值传递。
修改一下原形函数就行了
2009-09-13 16:11
jackwain
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:168
专家分:134
注 册:2009-3-21
收藏
得分:0 
结构是
struct test
{
   int num;
   struct  test *link;
};<---分号不能少
2009-09-13 16:12
jackwain
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:168
专家分:134
注 册:2009-3-21
收藏
得分:0 
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct node
{
    int num;
    char str[20];
    struct node *next;
};
int main()
{
    struct node *creat(struct node *);
    struct node *insert(struct node *,char *,int);
    struct node *delet(struct node *,char *);
    void print(struct node *);
2009-09-13 16:17
快速回复:来帮忙改,链表的。
数据加载中...
 
   



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

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