鱼C论坛

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

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

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

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

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

x
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct node)//定义节点的长度
#define num 10//定义产生随机数的个数
typedef int elemtype;//数据元素的类型
//定义结构体类型节点
struct node{
    elemtype data;//数据域
        struct node *next;//下一个节点
};                                                                                         
//初始化链表  申请内存空间
struct node *init(){
   struct node *head = (struct node*)malloc (LEN);
   if (head == NULL){
   printf("申请内存失败");
           exit(1);
   }
   head->next=NULL;
   return (head);
}
//元素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
        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 Traver(struct node *head){
                struct node *p=head->next;//跳过附加表头的头节点
                while(p!=NULL){
                printf("   %d",p->data);
                p=p->next;
                }
        }
        //创建单链表,其中有若干个随机数
void Creat(struct node *head){
                int i,j;
                for(i=1;i<=num;i++){
                j=rand();
                Insert(head,i,j);}
        }

void reverse(){
        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;
        
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void main()
{        int i=1;//循环控制变量
                struct node *h;//定义头指针
                h=init();//初始化链表
                while(i!=0)
                {
                        printf("\n  linked list example \n");
                        printf("1.creat 10 random number:\n");
                        printf("2.reverse the linked list \n");
                        printf("3.traver the linked list \n");
                        printf("0.exit the program .\n");
                        scanf("%d",&i);
                        switch(i)
                        {
                        case 0:exit(0);
                        case 1:Creat(h);break;
                        case 2:reverse(h);break;
                        case 3:Traver(h);break;
                        default:printf("input error !");
                        }
                }
} 





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-9-30 19:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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