| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3851 人关注过本帖
标题:链表初始化报错了
只看楼主 加入收藏
yang158
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2019-3-12
结帖率:16.67%
收藏
 问题点数:0 回复次数:1 
链表初始化报错了
这是源程序:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define Maxsize 50

typedef struct  phone {

    char name[20];
    char tel[15];

} ph;

struct node {

    ph data;
    struct node *next;

} Linklist;



int LinkInit(Linklist *L) {
    if( NULL == L) {    //若未定义实参则初始化失败
        return FAILURE;
    }

    (*L) = (Linklist *) malloc( sizeof( Linklist) * 1);
    (*L)->next = NULL;
    return SUCCESS;
}

void createlistF(Linklist *&L,ph a[],int n) {
    Linklist *s;
    int i;
    for(i=0; i<n; i++) {
        s=(Linklist *)malloc(sizeof(Linklist));
        s->data=a[i];
        s->next=L->next;
        L->next=s;
    }
}

bool createlistF1(Linklist *&L,ph a,int n) {
    int j=0;
    Linklist *p=L,*s;
    while(j<n-1&&p!=NULL) {
        j++;
        p=p->next;
    }
    if(p==NULL)  return false;
    else {
        s=(Linklist *)malloc(sizeof(Linklist));
        s->data=a;
        s->next=p->next;
        p->next=s;
        return true;
    }
}



bool ListDelete(Linklist*&L,int i, ph &x) {
    int j=0;
    Linklist *p=L,*q;
    while(j<i-1&&p!=NULL) {
        j++;
        p=p->next;
    }
    if(p==NULL)  return false;
    else {
        q=p->next;
        if(q==NULL)  return  false;
        x=q->data;
        p->next=q->next;
        free(q);
        return true;
    }
}


void Find(Linklist *Link) {
    if( NULL == Link) {
        printf("error 3\n");
        sleep(1);
        exit(1);
    }

    Linklist *current = Link;        // current     指向目标节点的前一个
    Linklist *target;                // target    记录找到的目标节点
    char name[20];

    printf("Press name:");
    scanf("%s", name);

    printf("Find result:\n");
    printf("\t%-20s %-20s\n", "Name", "Telephone number");

    while( current->next != NULL ) {
        target = current->next;
        if( 0 == strcmp(target->recoder.name, name)) {

            printf("\t%-20s %-20s\n", target->recoder.name, target->recoder.tel);
            printf("Find success.\n");
            sleep(1);
            return;
        }
        current = current->next;
    }
    printf("Can't find %s's infomation", name);
    fflush(stdout);
}
void DispList(Linklist *L) {
    Linklist *p=L->next;
    int i=1;
    printf("\n    -------------通讯录---------------\n");
    while(p!=NULL) {
        printf("      %d. %-20s%-10s\n",i,p->data.name,p->data.tel);
        p=p->next;
        i++;
    }
    printf("     -------------------------------\n");
}

void DestroyList(Linklist *&L) {
    L=(Linklist *)malloc(sizeof(Linklist));
    L->next=NULL;
}

void menu1() {
    printf("       ------------------------------\n");
    printf("          手机通讯录的应用(单链表)\n");
    printf("       ------------------------------\n");
    printf("             1.添加联系人信息\n");
    printf("             2.显示联系人信息\n");
    printf("             3.删除联系人信息\n");
    printf("             4.查找联系人信息\n");
    printf("             5.销毁通讯录\n");
    printf("             6.已退出!!\n");
    printf("       ------------------------------\n\n");
}

int main() {
    Linklist *h;
    int i;
    int flag=1;
    int j;
    ph s[17]= {"阿  光","13901210583","鲍大志","13001180515","高  倩","13001251186","陆  坚","13003133982","苏  瑾","13005166737",
               "蒋莉勤","13501245184","龚  湘","13501262620","李程元","13500002222","大  山","13503018291","刘  凯","13520004921","韩文静","13436789011",
               "黄 珊","13001141590","李圆圆","13001251186","李小婉","13001126879","何国华","18979788888","马  柯","13501204259","含  笑","13601008282"
              };
    ph x;
    menu1();
    InitList(h);
    createlistF(h,s,17);
    DispList(h);
    while(flag==1) {
        printf("请选择:");
        scanf("%d",&j);
        switch(j) {
            case 1:
                printf("请输入联系人姓名和电话号:");
                scanf("%s %s",x.name,x.tel);
                printf("请输入插入的位置:");
                scanf("%d",&i);
                fflush(stdin);
                printf("\n");
                createlistF1(h,x,i);
                break;
            case 2:
                DispList(h);
                break;
            case 3:
                printf("请输入删除的位置:");
                scanf("%d",&i);
                ListDelete(h,i,x);
                break;
            case 4:
                Find(L);
                break;
            case 5:
                DestroyList(h);
                printf("通讯录已销毁\n");
                break;
            case 6:
                flag=0;
                printf("\n     不再输入记录,退出!!\n\n");
                break;
        }
    }
    return 0;
}


结果运行的时候报错了
23    24    C:\Users\23080\Desktop\代码源.cpp    [Error] 'L' was not declared in this scope
23    27    C:\Users\23080\Desktop\代码源.cpp    [Error] expected ',' or ';' before '{' token

这个要怎么改啊
搜索更多相关主题的帖子: printf next int NULL name 
2020-04-22 19:29
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:0 
case 4:
                Find(L);
                break;
L 没有定义

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2020-05-10 10:44
快速回复:链表初始化报错了
数据加载中...
 
   



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

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