麦斯
版主
  
UID 1285
精华
0
积分 1678
帖子 1678
阅读权限 100
注册 2002-4-24
状态 离线
|
你的语法错误太多,不过思路都是对的,给你改了些比较明显的问题,你再试试吧.
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct LNode
{
DataType data;
struct LNode *next;
}LNode,*LinkedList;
LinkedList LinkedListInit() /*初始化链表 */
{
LinkedList L = (LNode*)malloc(sizeof(LNode));
if( L == NULL )
{
printf("\n Not enough space!");
exit(0);
}
L->next = NULL;
return L;
}
void LinkedListClear(LinkedList L) /*清空单链表 */
{
if( L == NULL ) return;
LinkedList P = L->next;
LinkedList Q = NULL;
while( P )
{
Q = P;
P = P->next;
free( Q );
}
}
int LinkedListEmpty( LinkedList L ) /*检查是否为空表*/
{
if( L->next == NULL )
{
printf("\n empty!");
return 1;
}
else
{
printf("\n Not empty!");
return 0;
}
}
void LinkedListTraverse(LinkedList L) /*遍历单链表*/
{
if( L == NULL ) return;
LinkedList P = L->next;
printf("\n");
while( P )
{
printf("%d ",P);
P = P->next;
}
}
int LinkedListLength(LinkedList L) /*求长度 */
{
if( L == NULL ) return -1;
LinkedList P = L->next;
int j = 0;
while( P )
{
j++;
P = P->next;
}
return j;
}
LinkedList LinkedListGet(LinkedList L,int i) /*按序查找 */
{
if( L == NULL ) return NULL;
if( i == 0 ) return L;
LinkedList P = L->next;
int j = 1;
while( P != NULL && j < i )
{
P = P->next;
j++;
}
if( j == i )
return P;
else
return NULL;
}
LinkedList LinkedListLocate(LinkedList L,DataType x) /*按值查找 */
{
LinkedList P = L->next;
P = L->next;
while( P != NULL && P->data != x )
{
P = P->next;
}
if( P->data == x ) return P;
else return NULL;
}
int LinkedListInsert(LinkedList L,int i,DataType x) /*在I之前插入元素X */
{
if( i <= 0 ) return -1;
LinkedList P = LinkedListGet( L, i-1 );
if( P == NULL ) return -1;
LinkedList N = (LinkedList)malloc( sizeof( LNode ) );
N->data = x;
N->next = P->next;
P->next = N;
return 0;
}
int LinkedListDel(LinkedList L,int i) /*删除第I个节点 */
{
if( i <= 0 ) return -1;
LinkedList P1 = LinkedListGet( L, i-1 );
LinkedList P2 = LinkedListGet( L, i );
if( P1 == NULL || P2 == NULL ) return -1;
P1->next = P2->next;
free( P2 );
return 0;
}
LinkedList LinkedListCreate( LinkedList L )
{
int x = 0;
scanf( "%d",&x );
LinkedList R = L;
while( 1 )
{
scanf( "%d", &x );
if( x == -1 ) break;
LinkedList P = (LNode*)malloc( sizeof(LNode) );
P->data = x;
P->next = NULL;
R->next = P;
R = P;
}
return L;
}
void main()
{
int n = 0;
int i = 0;
printf("Please input alphabet,ended with -1 !\n");
LinkedList L = LinkedListCreate( LinkedListInit() );
printf("\ncreate success, select operating:");
scanf( "%d", &n);
switch( n )
{
case 1:
LinkedListClear( L );
printf("The LinkedList has been cleared!");
break;
case 2:
LinkedListEmpty( L );
break;
case 3:
LinkedListTraverse( L );
break;
case 4:
printf("Please input the point you want to search!");
scanf( "%d", &i );
LinkedListGet( L, i );
break;
case 5:
printf("Please input the alphabet you want to search!");
scanf( "%d", &i );
LinkedListLocate( L, i );
break;
case 6:
printf("Please input the alhpabet you want to delete!");
scanf( "%d", &i );
LinkedListDel( L, i);
break;
}
}
|
|