| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 1451 人关注过本帖
标题:新建一个链表,并输出,大家看看我哪里错了
收藏  订阅  推荐  打印 
wshyj18
Rank: 2
等级:注册会员
帖子:124
积分:1368
注册:2007-5-14
新建一个链表,并输出,大家看看我哪里错了

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedel struct node
{
    int date;
    struct node *next;
}Snode,*Linklist;
void Creatlist(Linklist *L,int n)
{
    Linklist H,p;
L=(Linklist)malloc(sizeof(Snode));
p=L;
L->next=NULL;
for(int i=0;i<n;i++)
    {
     scanf("%d",&L->date);
     p->next=L;
     L=p;
    }
}
void print(Linklist *L)
{
while(p!=NULL)
    {
     printf("%d",L->date);
     L=L->next;
    }
}
main()
{
Linklist L;
Creatlist(&L,8)
    printf(&L);
}
搜索更多相关主题的帖子: 链表  输出  
2007-12-12 11:57
静思
Rank: 4
来自:沈阳
等级:高级会员
威望:8
帖子:623
积分:6412
注册:2006-2-28

楼主的这个程序错误好多呀,在Createlist()这个函数中变量声明好多都是有误的,在函数的形参中Linklist *L应为Linklist  L。下面是
void Creatlist(Linklist  L,int n)
{
    Linklist  L,p;
   L=(Linklist)malloc(sizeof(Snode));
  L->next=NULL;
for(int i=0;i<n;i++)
    {
     p=(Linklist)malloc(sizeof(Snode));
     scanf("%d",&p->date);
    p->next=L->next; //尾插法插入数据
    L->next=p;
    }
}
在Print这个函数中楼主还是自己看看错误吧,很明显的错误

英者自知,雄者自胜
2007-12-12 22:27
missiyou
Rank: 12Rank: 12Rank: 12
等级:版主
威望:7
帖子:312
积分:2853
注册:2007-10-9

typedef 这个也错了,还是别编了,看懂了,在去编
2007-12-14 20:01
scau
Rank: 1
等级:新手上路
帖子:17
积分:332
注册:2007-12-16
我改了一点 可以通过了

/*  HELLO.C -- Hello, world */

#include "stdio.h"
#include "alloc.h"
#include "stdlib.h"
#define LEN sizeof(struct node)
#define num 10
typedef int elemtype;
struct node
{
  elemtype data;
  struct node* next;
  };
  struct node* InitList()/**链表初始化函数**/
  {
  struct node* head=(struct node*)malloc(LEN);
  if(head==NULL)
  {
  printf("Memory allocation failure.\n");
  exit(1);
  }
    head->next=NULL;
    return (head);
  };

  int Insert(struct node* head,int i,elemtype x)  /**把元素x插入i位置处**/
  {
     struct node* p=head;
     struct node* newp;
     int count=0;
     if(i<1)
     {
     printf("Argyment error out of range!\n");
     return(0);
     }
     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);
     return(0);
        }
        newp=(struct node*)malloc(LEN);
        if(newp==NULL)
         {
         printf("Memory allocation failure.\n");
         return(0);
         }
      newp->data=x;
      newp->next=p->next;
      p->next=newp;
      return(1);
      }
      void Traverse(struct node* head)/**线性链表遍历函数**/
      {
      struct node* p=head->next;
      while(p!=NULL)
      {
        printf("%d",p->data);
        p=p->next;
        }
        }
  void create (struct node* head)/**创建线性表 其中存放一些数**/
{
   int i,j;
   for(i=1;i<=num;i++)
   {
   j=rand();/**调用产生函数**/
   Insert(head,i,j);
   }
   }

  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;
    }
    void main(){
    int i=1;
    struct node* h;
    h=InitList();
    while(i!=0)
    {
    printf("\n  Linked List Example   \n");
    printf("1.Create 10 random number;\n");
    printf("2.Reverse the linked List;\n");
    printf("3.Traverse the linked LIst:\n");
    printf("0.Exit the program.\n");
    printf("Please input your selection(0-3):");
    scanf("%d",&i);
    swich(i)
    {
    case 0:exit(0);
    case 1:create(h);break:
    case 2:reverse(h);break;
    case 3:Traverse(h);break;
    default:printf("input error!please slect again!");
    }
    }
    }
2007-12-18 17:40
scau
Rank: 1
等级:新手上路
帖子:17
积分:332
注册:2007-12-16
不好意思

不好意思啊  下面的那个是我的另外一个程序  我不小心把它给粘上去了 真是不好意思!
2007-12-18 17:43
scau
Rank: 1
等级:新手上路
帖子:17
积分:332
注册:2007-12-16
这个才是你要的那个程序 可以通过的了

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
    int date;
    struct node *next;
};

void Creatlist(struct node *L,int n)
{
int i;
    struct node *H,*p;
  L=(struct node*)malloc(sizeof(struct node));
  p=L;
  L->next=NULL;
  for( i=0;i<n;i++)
    {
    printf("Please input the %dth elem:\n",i+1);
     scanf("%d",&L->date);
     p->next=L;
     L=p;
    }
}
void print(struct node *L)
{
struct node *p=L;
while(p!=NULL)
    {
     printf("%d",L->date);
     L=L->next;
    }
}
main()
{
struct node *L;
Creatlist(&L,8);
    print(&L);
}
2007-12-18 17:46
zxc1998
Rank: 2
等级:注册会员
威望:1
帖子:133
积分:1444
注册:2007-3-21

不好意思,楼上的程序还是错误的,有时间请在改一下。
2007-12-18 20:34
ondy
Rank: 2
等级:注册会员
威望:1
帖子:88
积分:988
注册:2007-9-4

p->next=L;
L=p;

就写错了,

你可以写 p->next=l->next; l->next=p 或者 p->next=l;p=l

2007-12-18 22:33
cdj_cjf
Rank: 1
等级:新手上路
帖子:27
积分:370
注册:2008-7-16

http://bbs.palmjob.net/
2008-7-16 14:56
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.059492 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved