kkkkl 发表于 2017-9-22 10:38:34

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

#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("\nlinked 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        expected declaration or statement at end of input提示在main函数括号这里出错
哪位大神帮看一下~谢谢啦

weizhongyang 发表于 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;
      
}

weizhongyang 发表于 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;
      
}

kkkkl 发表于 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函数再遍历的时候只出来最后一个数
页: [1]
查看完整版本: C语言线性链表 函数的格式还是什么出了问题