| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 391 人关注过本帖
标题:链表的问题
只看楼主 加入收藏
滕方明
Rank: 1
等 级:新手上路
帖 子:17
专家分:4
注 册:2011-11-19
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:1 
链表的问题
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
    char name[10];
    int age;
    struct node *next;
}NODE;
NODE *create()
{
    NODE *head,*tail,*p;
    head=tail=NULL;
    p=(NODE *)malloc(sizeof(NODE));
    printf("please input a name.\n");
    scanf("%s",p->name);
   
    while(strcmp(p->name,"quit")!=0)
    {
        printf("please input age:\n");
    scanf("%d",&p->age);

    if(NULL==head)
        head=tail=p;
    else
        {
            tail->next=p;
            tail=p;
    }
    p=(NODE *)malloc(sizeof(NODE));
    printf("please input a name.\n");
    scanf("%s",p->name);
    }
    return head;
    }

int getlenth(NODE *head)
{
   NODE *p;
   p=head;
    int n=0;
   
    while(p!=NULL)
    {
        p=p->next;
        ++n;
    }
    return n;
}
void print(NODE *head)
{
    NODE *p;
    p=head;
    while(p!=NULL)
    {
        printf("name is %s age is %d\n",p->name,p->age);
        p=p->next;
    }
}
NODE* clear(NODE *head)
{
    NODE *p,*q;
    p=head;
    while(p)
    {
        q=p->next;
        free(p);
        p=q;
    }
    return p;
}
NODE * insert(NODE *head,int n)
{
    int j=0;
    NODE *p,*q;
    p=head;
    while((p!=NULL)&&(j<n))
    {
        p=p->next;
        j++;
    }
    if(p==NULL)
        printf("no node.\n");
    else
    {
        q=(NODE *)malloc(sizeof(NODE));
        printf("please inout the insert name:\n");
        scanf("%s",q->name);
        setbuf(stdin,NULL);
        printf("please input the insert age:\n");
        scanf("'%d",q->age);
        p->next=q;
        q->next=p->next;
    }
   
    return p;
}




    

int main(void)
{
    int n;
    int lenth;
    NODE *p;
   do
   {
       printf(" -------------1: create link:--------------\n");
       printf("|||||---------2: getlenth:-----------|||||\n");
       printf("|||||---------3:print:---------------|||||\n");
       printf("|||||---------4:clear:---------------|||||\n");
       printf("|||||---------5:insert:--------------|||||\n");
       printf("|||||---------6:search:--------------|||||\n");
       printf("--please input the number for what do you want to do.---\n");
    scanf("%d",&n);
    switch(n)
    {
        case 1:p=create();
               break;
        case 2:
               lenth=getlenth(p);
            printf("the link lenth is %d\n",lenth);
            break;
        case 3:
          print(p);
          break;
        case 4:
          p=clear(p);
        case 5:
            printf("please input where do you want wo insert the numer:\n");
            scanf("%d",&n);
            p=insert(p,n);
          break;
        case 6:
              printf("please input the number which you want to serch:\n");
              scanf("%d",&n);
        default:break;
    }
   }while(1);
    free(p);
}
插入新节点后  在重新打印  停不下来了   该怎么修改?还是我插入方法不对?
搜索更多相关主题的帖子: create 
2011-12-17 15:01
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:10 
你这个代码中出现两个问题:
1、申请空间没有初始化;
2、插入时,指针指向有误。
具体见如下代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
    char name[10];
    int age;
    struct node *next;
}NODE;
NODE *create()
{
    NODE *head,*tail,*p;
    head=tail=NULL;
    p=(NODE *)malloc(sizeof(NODE));
    memset(p, 0, sizeof(NODE));
    printf("please input a name.\n");
    scanf("%s",p->name);
   
    while(strcmp(p->name,"quit")!=0)
    {
        printf("please input age:\n");
    scanf("%d",&p->age);

    if(NULL==head)
        head=tail=p;
    else
        {
            tail->next=p;
            tail=p;
    }
    p=(NODE *)malloc(sizeof(NODE));
    memset(p, 0, sizeof(NODE));
    printf("please input a name.\n");
    scanf("%s",p->name);
    }
    return head;
    }

int getlenth(NODE *head)
{
   NODE *p;
    int n=0;

    p=head;   
    while(p!=NULL)
    {
        p=p->next;
        ++n;
    }
    return n;
}
void print(NODE *head)
{
    NODE *p;
    p=head;
    while(p!=NULL)
    {
        printf("name is %s age is %d\n",p->name,p->age);
        p=p->next;
    }
}
NODE* clear(NODE *head)
{
    NODE *p,*q;
    p=head;
    while(p)
    {
        q=p->next;
        free(p);
        p=q;
    }
    return p;
}
NODE *insert(NODE *head,int n)
{
    int j=0;
    NODE *p,*q;
    p=head;
    while((p!=NULL)&&(j<n))
    {
        p=p->next;
        j++;
    }
    if(p==NULL)
        printf("no node.\n");
    else
    {
        q=(NODE *)malloc(sizeof(NODE));
        memset(q, 0, sizeof(NODE));
        printf("please inout the insert name:\n");
        scanf("%s",q->name);
        setbuf(stdin,NULL);
        printf("please input the insert age:\n");
        scanf("%d", &q->age);
        q->next=p->next;
        p->next=q;
    }

    p = head;

    return p;
}




   

int main(void)
{
    int n;
    int lenth;
    NODE *p;
   do
   {
       printf(" -------------1: create link:--------------\n");
       printf("|||||---------2: getlenth:-----------|||||\n");
       printf("|||||---------3:print:---------------|||||\n");
       printf("|||||---------4:clear:---------------|||||\n");
       printf("|||||---------5:insert:--------------|||||\n");
       printf("|||||---------6:search:--------------|||||\n");
       printf("--please input the number for what do you want to do.---\n");
    scanf("%d",&n);
    switch(n)
    {
        case 1:p=create();
               break;
        case 2:
               lenth=getlenth(p);
            printf("the link lenth is %d\n",lenth);
            break;
        case 3:
          print(p);
          break;
        case 4:
          p=clear(p);
        case 5:
            printf("please input where do you want wo insert the numer:\n");
            scanf("%d",&n);
            p=insert(p,n);
          break;
        case 6:
              printf("please input the number which you want to serch:\n");
              scanf("%d",&n);
        default:break;
    }
   }while(1);
    free(p);
}

2011-12-17 16:19
快速回复:链表的问题
数据加载中...
 
   



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

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