wshyj18 发表于 2007-12-12 11:57

新建一个链表,并输出,大家看看我哪里错了

#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 22:27

楼主的这个程序错误好多呀,在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这个函数中楼主还是自己看看错误吧,很明显的错误

missiyou 发表于 2007-12-14 20:01

typedef 这个也错了,还是别编了,看懂了,在去编

scau 发表于 2007-12-18 17:40

我改了一点 可以通过了

/*  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!");
    }
    }
    }

scau 发表于 2007-12-18 17:43

不好意思

不好意思啊  下面的那个是我的另外一个程序  我不小心把它给粘上去了 真是不好意思!

scau 发表于 2007-12-18 17:46

这个才是你要的那个程序 可以通过的了

#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);
}

zxc1998 发表于 2007-12-18 20:34

不好意思,楼上的程序还是错误的,有时间请在改一下。

ondy 发表于 2007-12-18 22:33

p->next=L;
L=p;

就写错了,

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

cdj_cjf 发表于 2008-7-16 14:56

[tk02] [tk01] [tk03] [tk04] http://bbs.palmjob.net/

页: [1]

编程论坛