求助链表的问题
本帖最后由 夙惜言 于 2020-11-16 19:41 编辑感觉自己快忘干净了,一直报错:
In function 'main':
expected declaration specifiers or '...' before '&' token
expected declaration specifiers or '...' before numeric constant
附上原码:
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
//¹ØÓÚElemType:ËüÊÇelement type£¨¡°ÔªËصÄÀàÐÍ¡±£©µÄ¼ò»¯Ìå¡£
//¿ÉÒÔÊÇÈκÎÊý¾ÝÀàÐÍ£¬ÕâÀïÊÇintÐÍ
typedef struct Node
{
int data;
struct Node * next;
}Node;
typedef struct Node* LinkList;
//link:Á´½Ó list:Ŀ¼
int GetElem (LinkList L,int i,int *e);
//·µ»ØÁ´±íLµÚi¸öÊý¾Ý
int ListInsert (LinkList L,int i,int e);
//½«ÔªËØe²åÈëÁ´±íLµÄµÚi¸öλÖÃ
int ListDelete (LinkList L,int i,int *e);
//ɾ³ýÁ´±íLµÄµÚi¸öλÖõÄÊýÖµ£¬²¢ÓÃe·µ»ØÆäÖµ
LinkList CreateListHead ();
//´´½¨Ò»¸öÁ´±í¡ª¡ªÍ·²å·¨
int main (void)
{
LinkList *L;
LinkList CreateListHead(&L,3);
return 0;
}
int GetElem (LinkList L,int i,int *e)
{
int j;
LinkList p = L->next;
for(j = 1;p && j <= i;j++)
//°É¡®p¡¯·ÅÔÚÅж¨½á¹ûÀÈç¹û¡®p¡¯ÎªNULL£¬ÄÇô¡®&&¡¯·µ»ØΪ0£¬³ÌÐò½áÊø
//ÕâÑùµÄºÃ´¦¾ÍÊÇÊ¡È¥ÁËÒ»´ÎifÅж¨
{
p = p->next;
}
if( !p || j>i )
{
return 0;
}
*e = p->data;
return 1;
}
int ListInsert (LinkList L,int i,int e)
{
int j;
LinkList _new;
LinkList p = L->next;
while( p && j<i )
{
p = p->next;
j++;
}
if( !p || j>i )
{
return 0;
}
_new = (LinkList)malloc(sizeof(Node));
_new->data = e;
_new->next = p->next;
p->next = _new;
return 1;
}
int ListDelete (LinkList L,int i,int *e)
{
int j;
LinkList p = L;
for(j = 0;j < i && p;j++)
{
p = p->next;
}
if( !(p->next) || j>i )
{
return 0;
}
p = p->next->next;
*e = p->next->data;
free(p->next);
return 1;
}
LinkList CreateListHead( LinkList *L,int number )
{
LinkList p;
int newdata;
(*L) = (LinkList)malloc(sizeof(Node));
(*L)->next = NULL;
for( int i = 1;i < number;i++ )
{
p = (LinkList)malloc(sizeof(Node));
scanf("%d",&newdata);
p -> data = newdata;
p -> next = (*L)->next;
(*L) -> next = p;
}
}
本帖最后由 夙惜言 于 2020-11-16 19:36 编辑
不知道为什么我的备注变成了一片乱码
CreateListHead 和 main 应该这么写.
void CreateListHead(LinkList *L,int number );
int main (void)
{
LinkList L;
CreateListHead(&L,3);
return 0;
}
void CreateListHead(LinkList *L,int number )
{
LinkList p;
int newdata;
(*L) = (LinkList)malloc(sizeof(Node));
(*L)->next = NULL;
for( int i = 1;i < number;i++ )
{
p = (LinkList)malloc(sizeof(Node));
scanf("%d",&newdata);
p -> data = newdata;
p -> next = (*L)->next;
(*L) -> next = p;
}
}
页:
[1]