| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2520 人关注过本帖
标题:新建一个链表,并输出,大家看看我哪里错了
只看楼主 加入收藏
wshyj18
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2007-5-14
收藏
 问题点数:0 回复次数:8 
新建一个链表,并输出,大家看看我哪里错了
#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: 3Rank: 3
来 自:沈阳
等 级:新手上路
威 望:8
帖 子:630
专家分:0
注 册:2006-2-28
收藏
得分:0 
楼主的这个程序错误好多呀,在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: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:0 
typedef 这个也错了,还是别编了,看懂了,在去编
2007-12-14 20:01
scau
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-12-16
收藏
得分:0 
我改了一点 可以通过了
/*  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
专家分:0
注 册:2007-12-16
收藏
得分:0 
不好意思
不好意思啊  下面的那个是我的另外一个程序  我不小心把它给粘上去了 真是不好意思!
2007-12-18 17:43
scau
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-12-16
收藏
得分:0 
这个才是你要的那个程序 可以通过的了
#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: 1
等 级:新手上路
威 望:1
帖 子:133
专家分:0
注 册:2007-3-21
收藏
得分:0 
不好意思,楼上的程序还是错误的,有时间请在改一下。
2007-12-18 20:34
ondy
Rank: 1
等 级:新手上路
威 望:1
帖 子:88
专家分:0
注 册:2007-9-4
收藏
得分:0 
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
专家分:0
注 册:2008-7-16
收藏
得分:0 
http://bbs.
2008-07-16 14:56
快速回复:新建一个链表,并输出,大家看看我哪里错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016613 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved