XiaoHui.net 笑汇程序员论坛首页
工作并快乐着,职业并休闲着
寻梦的岁月从不言辛苦几许,
不问收获几多……
» 游客:  申请新用户 | 登录 | 会员 | 统计 | 帮助 » XiaoHui.Net 笑汇程序员论坛 | 纯文字版 | 全站索引 | XiaoHui.com


[算法讨论] [求助]一个链表的基本操作程序,有错误

RSS 订阅当前论坛  

上一主题 下一主题
     

标题: [算法讨论] [求助]一个链表的基本操作程序,有错误  
 
hywok
小水手
Rank: 1



UID 25336
精华 0
积分 1
帖子 1
阅读权限 10
注册 2006-11-15
状态 离线
[求助]一个链表的基本操作程序,有错误

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

#include "stdio.h"
#include "conio.h"
typedef int DataType;

  typedef struct LNode
  {DataType data;
    struct LNode *next;
    }LNode,*LinkedList;


LinkedList LinkedListInit()         /*初始化链表  */
{ L=(LNode*)malloc(sizeof(LNode));
  if(L==null)
  {printf("Not enough space!");
    exit(0);
    }
    L->next=null;
    return L;
    }

Void LinkedListClear(LinkedList L) /*清空单链表  */
{P=L;int Q;
  while(P->next)
       {Q=P->next;
        P->next=Q->next;
        free(Q);
        }

int LinkedListEmpty(LinkedList L)   /*检查是否为空表*/                                   */
{if(L->next==null)
   printf("empty!");
   return 1;
  else printf("Not empty!");
       return 0;
       }

void LinkedListTraverse(LinkedList L)  /*遍历单链表*/
{P=L;
while(P->next)
      {printf("%c",P);
       P=P->next;
       }
  }

int LinkedListLength(LinkedList L)  /*求长度  */
{  P=L->next;j=0;
    while(P->next)
     {j++=P->next;}
      return j;
      }

LinkedList LinkedListGet(LinkedList L,int i)  /*按序查找   */
{P=L->next;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) /*按值查找  */
{ P=L->next;j=1;
   if(P!=null&&->data!=x)
      {P=P->next;j++;
      }
   if(p->next==x)return j;
     else return 0;
     }

void LinkedListInsert(LinkedList L,int i,DataType x)  /*在I之前插入元素X */
{pre=L;
  while(pre!=null&&pre->next!=i)
  pre=pre->next;
  if(!pre){printf("The insert place is wrong!");exit(0);}
    S=(LNode*)malloc(sizeof(LNode)) ;
    S->data=x;
    S->next=pre->next;
    pre->next=S;
    }

void LinkedListDel(LinkedList L,int i)  /*删除第I个节点  */
{P=L;j=1;
   while(P->next&&j>i)
        {P=P->next;j++;}
   if(P->next==null||j>i)
   {printf("The point you want to delete is wrong!");
      exit(0);
      }
   Q=P->next->next=Q->next;free(Q);
   }

  LinkedList LinkedListCreat()
  { scanf(&x);
    while(x!=flag)
         {P=(LNode*)malloc(sizeof(LNode));
         P->data=x; r->next=P;r=P;
         scanf(&x);
         }
   r->next=null;return L;
   }


main()
{int n,i
   printf("Please input alphabet,ended with "flag"!");
   LinkedList LinkedListCreat();
   scanf(&n);
  switch(n)
  {case 1inkedListClear(LinkedList L);
           printf("The LinkedList has been cleared!");
   case 2inkedListEmpty(LinkedList l);
   case 3inkedListTravers(LinkedList L);
   case 4:printf("Please input the point you want to search!");
          scanf(&i);
          LinkedListGet(LinkedList L,int i);
   case 5:printf("Please input the alphabet you want to search!");
          scanf(&i);
          LinkedListLocate(LinkedList L;DataType x)
   case 6:printf("Please input the alhpabet you want to delete!");
          scanf(&x);
          LinkedListDel(LinkedList L,int i);
          }
}

里面有一些错误,但是自己找不出来,请教同学也不会,只好来看看有没有高手帮忙调试一下了。有哪位高手帮帮小弟调试通过呀,谢谢了
2006-11-15 20:39#1
查看资料  Blog  发短消息  顶部
 
qihong
小水手
Rank: 1


UID 26107
精华 0
积分 8
帖子 8
阅读权限 10
注册 2006-11-29
来自 吉林省长春市
状态 离线
我没时间等有时间的吧

好好学习天天向上
2006-11-29 14:32#2
查看资料  Blog  发短消息  QQ  顶部
 
xp20031119
传说中的水手
Rank: 5Rank: 5



UID 25060
精华 0
积分 24
帖子 24
阅读权限 1
注册 2006-11-9
状态 离线
我晕
没有定义就去使用
#include "stdio.h"
#include "conio.h"
typedef int DataType;

  typedef struct LNode
  {DataType data;
    struct LNode *next;
    }LNode,*LinkedList;


void LinkedListInit()         /*初始化链表  */
{ L=(LNode*)malloc(sizeof(LNode));
  if(L==null)
  {
        printf("Not enough space!");
        exit(0);
  }
    L->next=null;
}

void LinkedListClear(LinkedList *L) /*清空单链表  */
{LinkedList *Q,*p;
  P=L->next;
  while(P->next)
       {
          Q->next=P->next;
      P=p->next;
      free(Q);
       }
}

int LinkedListEmpty(LinkedList *L)   /*检查是否为空表*/                                   */
{
        LinkedList *head;
        head=L->next;
        int i=0;
        while(head)
        {
                i++;
                head=head->next;
        }
        if(i!=0)
        {
                printf("not emtpy");
                return 1;
        }
        else
        {
                printf("empty");
                return 0;
        }
}

void LinkedListTraverse(LinkedList *L)  /*遍历单链表*/
{
       
        LinkedList *p;
                P=L->next;
   while(P)
      {printf("%c",P->data);
       P=P->next;
       }
  }

int LinkedListLength(LinkedList *L)  /*求长度  */
{  
        LinkedList *p;
        P=L->next;
        int j=0;
    while(P)
     {j++;
        p=P->next;
        }
      return j;
}

int LinkedListGet(LinkedList *L,int i)  /*按序查找   */
{
        LinkedList *p;
        P=L->next;
        int j=1;
   while(P!=null&&j<i)
        {P=P->next;
          j++;
          }
   if(j==i)
     return 1;
   else
         return 0;
   }

int  LinkedListLocate(LinkedList L,DataType x) /*按值查找  */
{ LinkedList *p;
  P=L->next;
  int j=0;
   if(P!=null&&p->data==x)
      {
           P=P->next;
           j++;
      }
   if(p->next==x)
           return j;
     else return 0;
     }

void LinkedListInsert(LinkedList L,int i,DataType x)  /*在I之前插入元素X */
{pre=L;
  while(pre!=null&&pre->next!=i)
  pre=pre->next;
  if(!pre){printf("The insert place is wrong!");exit(0);}
    S=(LNode*)malloc(sizeof(LNode)) ;
    S->data=x;
    S->next=pre->next;
    pre->next=S;
    }

void LinkedListDel(LinkedList *L,int i)  /*删除第I个节点  */
{LinkedList *p;
        P=L->next;
        int j=0;
   while(P->next&&p->data==i)
        {P-next=P->next-next;
                j++;}
                printf("删掉个结点数",j);
}

void LinkedListCreat(LinkedList *L)
  { scanf("%d",&x);
        LinkedList *p;
    while(x)
         {P=(LNode*)malloc(sizeof(LNode));
         P->data=x;
                 L->next=p;
                 p->next=L->next;
                 L->next->data=p->data;
         scanf("%d",&x);
         }
   }


main()
{int n,i,x;
LinkedList L;
   printf("Please input alphabet,ended with "flag"!");
   LinkedListCreat(L);
   scanf("%d",&n);
  switch(n)
  {case 1:
                   inkedListClear(L);
           printf("The LinkedList has been cleared!");break;
   case 2:
           inkedListEmpty( L);break;
   case 3:
           inkedListTravers( 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,x);
                  break;
   case 6:
           printf("Please input the alhpabet you want to delete!");
          scanf("%d",&x);
          LinkedListDel( L,int i);
                  break;
          }
}
2006-11-29 21:54#3
查看资料  Blog  发短消息  顶部
 
麦斯
版主
Rank: 7Rank: 7Rank: 7



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;
  }
}
2006-12-13 21:31#4
查看资料  Blog  发短消息  顶部
 
xp20031119
传说中的水手
Rank: 5Rank: 5



UID 25060
精华 0
积分 24
帖子 24
阅读权限 1
注册 2006-11-9
状态 离线
没意思,里面的人太菜了,哎
2006-12-13 22:25#5
查看资料  Blog  发短消息  顶部
 
zj41342626
小水手
Rank: 1



UID 27690
精华 0
积分 3
帖子 3
阅读权限 10
注册 2006-12-24
来自 江西吉安
状态 离线
我顶你 xp20031119
你牛B的话,你不做一个操作系统出来了????
2006-12-28 23:34#6
查看资料  访问主页  Blog  发短消息  QQ  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题  


 


所有时间为 GMT+8, 现在时间是 2008-11-23 03:55 Powered by Discuz! 4.1.0 清除 Cookies - XiaoHui.Net 笑汇程序员论坛 - Archiver