鱼C论坛

 找回密码
 立即注册
查看: 2239|回复: 3

[已解决]大二小白求助 数据结构单链表的插入和删除一直报错,看不明白啊

[复制链接]
发表于 2019-10-19 14:44:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
就写了这么一点
好多错误,哭了
#include <stdlib.h>
#include <stdio.h>
typedef int ElemType;
typedef struct LNode{
        ElemType data;
        struct LNode *next;
}LNode,*LinkList;
void CreateList_L(LinkList &L,int n){
        int i;
        L=(LinkList)malloc(sizeof (LNode));
    L->next=NULL;
        for(i=n;i>0;--i){
    p=(LinkList)malloc(sizeof (LNode));
        scanf(&p->data);
        p->next=L->next;L->next=p;
        }


下面是编译时候的报错:
E:\nue\2.cpp(13) : error C2065: 'p' : undeclared identifier
E:\nue\2.cpp(13) : error C2440: '=' : cannot convert from 'struct LNode *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
E:\nue\2.cpp(14) : error C2227: left of '->data' must point to class/struct/union
E:\nue\2.cpp(15) : error C2227: left of '->next' must point to class/struct/union
E:\nue\2.cpp(15) : error C2440: '=' : cannot convert from 'int' to 'struct LNode *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\nue\2.cpp(18) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.
最佳答案
2019-10-19 15:12:32
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef int ElemType       ;

  4. typedef struct node {
  5.         ElemType data      ;
  6.         struct node * next ;
  7. } LNode , * LinkList       ;

  8. void CreateList_L(LinkList * L , int n)
  9. {
  10.         int i                                                  ;
  11.         LinkList p , q                                         ;
  12.         for(i = 0 ; i < n ; i ++) {
  13.                 if((p = (LinkList)malloc(sizeof (LNode)))) {
  14.                         scanf("%d" , & p -> data)              ;
  15.                         if(! i) {
  16.                                 p -> next = p                  ;
  17.                                 * L = p                        ;
  18.                         } else {
  19.                                 p -> next = * L                ;
  20.                                 q -> next = p                  ;
  21.                         }
  22.                         q = p                                  ;
  23.                 } else {
  24.                         * L = NULL                             ;
  25.                         fprintf(stderr , "\n\n")               ;
  26.                         fprintf(stderr , "Error : malloc()\n") ;
  27.                 }
  28.         }
  29. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-19 15:06:54 | 显示全部楼层
对啊。你的变量p,在哪里定义了?没有定义,所以报错。
你定一下就可以了。
不要那么轻易的放弃。其实你距离成功,就差那么一点点了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-19 15:12:32 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef int ElemType       ;

  4. typedef struct node {
  5.         ElemType data      ;
  6.         struct node * next ;
  7. } LNode , * LinkList       ;

  8. void CreateList_L(LinkList * L , int n)
  9. {
  10.         int i                                                  ;
  11.         LinkList p , q                                         ;
  12.         for(i = 0 ; i < n ; i ++) {
  13.                 if((p = (LinkList)malloc(sizeof (LNode)))) {
  14.                         scanf("%d" , & p -> data)              ;
  15.                         if(! i) {
  16.                                 p -> next = p                  ;
  17.                                 * L = p                        ;
  18.                         } else {
  19.                                 p -> next = * L                ;
  20.                                 q -> next = p                  ;
  21.                         }
  22.                         q = p                                  ;
  23.                 } else {
  24.                         * L = NULL                             ;
  25.                         fprintf(stderr , "\n\n")               ;
  26.                         fprintf(stderr , "Error : malloc()\n") ;
  27.                 }
  28.         }
  29. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-19 15:32:48 | 显示全部楼层
wwhywhy 发表于 2019-10-19 15:06
对啊。你的变量p,在哪里定义了?没有定义,所以报错。
你定一下就可以了。
不要那么轻易的放弃。其实你 ...

嗯嗯,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 04:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表