鱼C论坛

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

[已解决]C语言线性链表 函数的格式还是什么出了问题

[复制链接]
发表于 2017-9-22 10:38:34 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. #define LEN sizeof(struct node)//定义节点的长度
  5. #define num 10//定义产生随机数的个数
  6. typedef int elemtype;//数据元素的类型
  7. //定义结构体类型节点
  8. struct node{
  9.     elemtype data;//数据域
  10.         struct node *next;//下一个节点
  11. };                                                                                         
  12. //初始化链表  申请内存空间
  13. struct node *init(){
  14.    struct node *head = (struct node*)malloc (LEN);
  15.    if (head == NULL){
  16.    printf("申请内存失败");
  17.            exit(1);
  18.    }
  19.    head->next=NULL;
  20.    return (head);
  21. }
  22. //元素x插入到i的位置
  23. int Insert(struct node *head,int i, elemtype x){
  24.         //当前访问节点
  25.         struct node *p=head;
  26.         struct node *newp;//新增节点
  27.         int count;//计数器       
  28.         if(i<1){
  29.         printf("Argument is error,out of range");//参数范围出错
  30.         return(0);//返回假值表示不成功
  31.         //遍历链表 找到i-1节点
  32.         while(p!=NULL&&count<i-1){
  33.         p=p->next;
  34.         count++;
  35.         }
  36.         if(p==NULL)
  37.         {
  38.         printf("the length of the linked list is less than %d.\n,i-1");
  39.         //需要找到i-1节点 但是链表的长度小于i-1
  40.         return (0);//插入不成功
  41.         }
  42.         //给新节点申请空间
  43.         struct node *newp = (struct node*)malloc (LEN);
  44.         if(newp== NULL){
  45.         printf("申请失败");
  46.         exit(1);}
  47.         //插入新节点  指针指向 数据域插入
  48.         newp->data=x;
  49.         newp->next=p->next;
  50.         p->next=newp;
  51.         return(1);//插入成功;

  52. }
  53.         //遍历函数
  54. void Traver(struct node *head){
  55.                 struct node *p=head->next;//跳过附加表头的头节点
  56.                 while(p!=NULL){
  57.                 printf("   %d",p->data);
  58.                 p=p->next;
  59.                 }
  60.         }
  61.         //创建单链表,其中有若干个随机数
  62. void Creat(struct node *head){
  63.                 int i,j;
  64.                 for(i=1;i<=num;i++){
  65.                 j=rand();
  66.                 Insert(head,i,j);}
  67.         }

  68. void reverse(){
  69.         struct node *cp=head->next;//当前指针,指向当前正处理的节点
  70.         struct node *pp=NULL;//指向当前节点的前驱
  71.         struct node *np;//指向当前节点的后驱
  72.         while(cp!=NULL){
  73.                 np=cp->next;
  74.                 cp->next==pp;
  75.                 pp=cp;
  76.                 cp=np;
  77.         }
  78.         head->next=pp;
  79.        
  80. }
  81. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  82. void main()
  83. [color=Red]{[/color]        int i=1;//循环控制变量
  84.                 struct node *h;//定义头指针
  85.                 h=init();//初始化链表
  86.                 while(i!=0)
  87.                 {
  88.                         printf("\n  linked list example \n");
  89.                         printf("1.creat 10 random number:\n");
  90.                         printf("2.reverse the linked list \n");
  91.                         printf("3.traver the linked list \n");
  92.                         printf("0.exit the program .\n");
  93.                         scanf("%d",&i);
  94.                         switch(i)
  95.                         {
  96.                         case 0:exit(0);
  97.                         case 1:Creat(h);break;
  98.                         case 2:reverse(h);break;
  99.                         case 3:Traver(h);break;
  100.                         default:printf("input error !");
  101.                         }
  102.                 }
  103. [color=Red]}[/color]
复制代码






105        1        E:\数据结构学习\线性链表.c        [Error] expected declaration or statement at end of input  提示在main函数括号这里出错
哪位大神帮看一下~谢谢啦
最佳答案
2017-9-22 11:40:26
weizhongyang 发表于 2017-9-22 11:34
你这程序有好几处错误
我帮你修改了这几处,详细修改如下(红色部分),其它不变,你再编译试试

看这个吧,更新了:
//元素x插入到i的位置
int Insert(struct node *head,int i, elemtype x){
        //当前访问节点
        struct node *p=head;
       //struct node *newp;//新增节点     /*这里就不要定义了,下面会有重新定义的地方*/
        int count;//计数器        
        if(i<1){
        printf("Argument is error,out of range");//参数范围出错
        return(0);//返回假值表示不成功
                }   /*少了这个*/
        //遍历链表 找到i-1节点
        while(p!=NULL&&count<i-1){
        p=p->next;
        count++;
        }
        if(p==NULL)
        {
        printf("the length of the linked list is less than %d.\n",i-1);  /*打印是不是需要看i-1的值呢?*/
        //需要找到i-1节点 但是链表的长度小于i-1
        return (0);//插入不成功
        }
        //给新节点申请空间
        struct node *newp = (struct node*)malloc (LEN);
        if(newp== NULL){
        printf("申请失败");
        exit(1);}
        //插入新节点  指针指向 数据域插入
        newp->data=x;
        newp->next=p->next;
        p->next=newp;
        return(1);//插入成功;

}

void reverse(struct node *head){   /*需要带参数*/
        struct node *cp=head->next;//当前指针,指向当前正处理的节点
        struct node *pp=NULL;//指向当前节点的前驱
        struct node *np;//指向当前节点的后驱
        while(cp!=NULL){
                np=cp->next;
                cp->next==pp;
                pp=cp;
                cp=np;
        }
        head->next=pp;
        
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-22 11:34:10 | 显示全部楼层
你这程序有好几处错误
我帮你修改了这几处,详细修改如下(红色部分),其它不变,你再编译试试

int Insert(struct node *head,int i, elemtype x){
        //当前访问节点
        struct node *p=head;
        //struct node *newp;//新增节点    /*下面已经有声明和定义了,在这去掉*/
        int count;//计数器        
        if(i<1){
        printf("Argument is error,out of range");//参数范围出错
        return(0);//返回假值表示不成功
                }      /*少}这个*/
        //遍历链表 找到i-1节点
        while(p!=NULL&&count<i-1){
        p=p->next;
        count++;
        }
        if(p==NULL)
        {
        printf("the length of the linked list is less than %d.\n",i-1);
        //需要找到i-1节点 但是链表的长度小于i-1
        return (0);//插入不成功
        }
        //给新节点申请空间
        struct node *newp = (struct node*)malloc (LEN);
        if(newp== NULL){
        printf("申请失败");
        exit(1);}
        //插入新节点  指针指向 数据域插入
        newp->data=x;
        newp->next=p->next;
        p->next=newp;
        return(1);//插入成功;

}


void reverse(struct node *head){    /*缺少参数啊*/
        struct node *cp=head->next;//当前指针,指向当前正处理的节点
        struct node *pp=NULL;//指向当前节点的前驱
        struct node *np;//指向当前节点的后驱
        while(cp!=NULL){
                np=cp->next;
                cp->next==pp;
                pp=cp;
                cp=np;
        }
        head->next=pp;
        
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-22 11:40:26 | 显示全部楼层    本楼为最佳答案   
weizhongyang 发表于 2017-9-22 11:34
你这程序有好几处错误
我帮你修改了这几处,详细修改如下(红色部分),其它不变,你再编译试试

看这个吧,更新了:
//元素x插入到i的位置
int Insert(struct node *head,int i, elemtype x){
        //当前访问节点
        struct node *p=head;
       //struct node *newp;//新增节点     /*这里就不要定义了,下面会有重新定义的地方*/
        int count;//计数器        
        if(i<1){
        printf("Argument is error,out of range");//参数范围出错
        return(0);//返回假值表示不成功
                }   /*少了这个*/
        //遍历链表 找到i-1节点
        while(p!=NULL&&count<i-1){
        p=p->next;
        count++;
        }
        if(p==NULL)
        {
        printf("the length of the linked list is less than %d.\n",i-1);  /*打印是不是需要看i-1的值呢?*/
        //需要找到i-1节点 但是链表的长度小于i-1
        return (0);//插入不成功
        }
        //给新节点申请空间
        struct node *newp = (struct node*)malloc (LEN);
        if(newp== NULL){
        printf("申请失败");
        exit(1);}
        //插入新节点  指针指向 数据域插入
        newp->data=x;
        newp->next=p->next;
        p->next=newp;
        return(1);//插入成功;

}

void reverse(struct node *head){   /*需要带参数*/
        struct node *cp=head->next;//当前指针,指向当前正处理的节点
        struct node *pp=NULL;//指向当前节点的前驱
        struct node *np;//指向当前节点的后驱
        while(cp!=NULL){
                np=cp->next;
                cp->next==pp;
                pp=cp;
                cp=np;
        }
        head->next=pp;
        
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-23 09:44:41 | 显示全部楼层
weizhongyang 发表于 2017-9-22 11:40
看这个吧,更新了:
//元素x插入到i的位置
int Insert(struct node *head,int i, elemtype x){

谢谢了~还是小白一枚。想要好好学数据结构  但是感觉好难。。

不过好像我的reverse函数出了点问题  再调用reverse函数再遍历的时候只出来最后一个数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 14:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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