| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 358 人关注过本帖
标题:求高手速回,在数据结构课本上的程序
只看楼主 加入收藏
youaadd
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2010-1-13
结帖率:50%
收藏
 问题点数:0 回复次数:3 
求高手速回,在数据结构课本上的程序
程序:
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct Node
{
   DataType data;   
   struct Node *next;   
} SLNode;

void LiistInitiate(SLNode **head)
{
    *head=(SLNode*)malloc(sizeof(SLNode));
    (*head)->next=NULL;
}

void DestroyList(SLNode **head)

{
    SLNode *pl,*P=*head;   
        while(P != NULL)
        {
            pl = P;
            P= p->next;
      free(pl);
        }
        *head=NULL;
    }

int ListGet(SLNode *head,int i,DataType *x)
{
   SLNode *p;
   int j;
    p=head;
    j=-1;
    while(P->next!=NULL && j<i)
        {P = P->next;j++;}
    if(j!=i)
        {printf("error");return 0;};
    *x=p->data;
    return 1;
}

int ListDelete(SLNode *head,int i,DataType *x)
{
   SLNode *p,*s;
   int j;
   p=head;
   j=-1;
while(p->next!=NULL&&p->next->next!=NULL&&j<i-1)
    {P= P->next;
      j++;
     }
   if(j!=i-1)
  {
    printf("error");
    return 0;
  }
  s=p->next;
  *x=s->data;
  p->next=p->next->next;
 free(s);
 return 1;
}

int ListInsert(SLNode *head, int i,DaataType x)
{
    SLNode *p,*q;
    int j;
   p=*head;
   j=-1;
  while(p->next!-NULL&&j<i-1)
 {
   p=p->next;
   j++;
 }
  if(j!=i-1)
  {printf("error");return 0;}
  q=(SLNode*)malloc(sizeof(SLNode));
  q->data=x;
  q->next=p->next;
  p->next=q;
  return 1;
}

void main(void)
{
    int i, x;
    SLNode *head;
   
    ListInitiate(&head);
   
    for (i=0; i<10; i++)
        ListInsert(head,i,i+1);
    ListDelete(head,4,&x);
                for(i=0;i<ListLength(head);i++)
                {
                    ListGet(head,i,&x);
                    printf("%d    ",x);
                }
    DestroyList(&head);
}
编译的结果:
F:\1\110620\复件 1.c(24) : error C2065: 'p' : undeclared identifier
F:\1\110620\复件 1.c(24) : error C2223: left of '->next' must point to struct/union
F:\1\110620\复件 1.c(36) : error C2065: 'P' : undeclared identifier
F:\1\110620\复件 1.c(36) : error C2223: left of '->next' must point to struct/union
F:\1\110620\复件 1.c(37) : error C2223: left of '->next' must point to struct/union
F:\1\110620\复件 1.c(51) : error C2223: left of '->next' must point to struct/union
F:\1\110620\复件 1.c(67) : error C2146: syntax error : missing ')' before identifier 'x'
F:\1\110620\复件 1.c(67) : error C2081: 'DaataType' : name in formal parameter list illegal
F:\1\110620\复件 1.c(67) : error C2061: syntax error : identifier 'x'
F:\1\110620\复件 1.c(67) : error C2059: syntax error : ';'
F:\1\110620\复件 1.c(67) : error C2059: syntax error : ')'
F:\1\110620\复件 1.c(68) : error C2449: found '{' at file scope (missing function header?)
F:\1\110620\复件 1.c(85) : error C2059: syntax error : '}'
执行 cl.exe 时出错.
搜索更多相关主题的帖子: 课本 
2011-06-20 16:47
youaadd
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2010-1-13
收藏
得分:0 
回复 楼主 youaadd
p是我明明有定义为什么还是……杯具收场,已经玩了两天了,自己学真是不容易啊……
2011-06-20 16:50
youaadd
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2010-1-13
收藏
得分:0 
没人帮我啊?
2011-06-20 19:43
youaadd
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2010-1-13
收藏
得分:0 
刚刚找到了……自己答!!!!!
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct Node
{
   DataType data;        
   struct Node *next;   
}SLNode;

void ListInitiate(SLNode **head)
{
    *head=(SLNode*)malloc(sizeof(SLNode));
    (*head)->next=NULL;
}

int ListLength(SLNode *head)
{
    SLNode *p=head;
    int size=0;
    while(p->next!=NULL)
    {
        p=p->next;
        size++;
    }
    return size;
}

void DestroyList(SLNode **head)

{
   SLNode *pl,*p;
   p=*head;
        while(p != NULL)
        {
            pl =p;
            p= p->next;
      free(pl);
        }
        *head=NULL;
    }

int ListGet(SLNode *head,int i,DataType *x)
{
   SLNode *p;
   int j;
    p=head;
    j=-1;
    while(p->next!=NULL && j<i)
        {p = p->next;j++;}
    if(j!=i)
        {printf("error");return 0;};
    *x=p->data;
    return 1;
}

int ListDelete(SLNode *head,int i,DataType *x)
{
   SLNode *p,*s;
   int j;
   p =head;
   j=-1;
while(p->next!=NULL&&p->next->next!=NULL&&j<i-1)
    {p= p->next;
      j++;
     }
   if(j!=i-1)
  {
    printf("error");
    return 0;
  }
  s=p->next;
  *x=s->data;
  p->next=p->next->next;
 free(s);
 return 1;
}


int ListInsert(SLNode *head, int i,DataType x)
{
    SLNode *p,*q;
    int j;
   p=head;
   j=-1;
  while(p->next!=NULL&&j<i-1)
 {
   p=p->next;
   j++;
 }
  if(j!=i-1)
  {printf("error");return 0;}
  q=(SLNode*)malloc(sizeof(SLNode));
  q->data=x;
  q->next=p->next;
  p->next=q;
  return 1;
}

void main(void)
{
    int i, x;
    SLNode *head;
   
    ListInitiate(&head);
   
    for (i=0; i<10; i++)
        ListInsert(head,i,i+1);
    ListDelete(head,4,&x);
                for(i=0;i<ListLength(head);i++)
                {
                    ListGet(head,i,&x);
                    printf("%d    ",x);
                }
    DestroyList(&head);
}
2011-06-20 20:01
快速回复:求高手速回,在数据结构课本上的程序
数据加载中...
 
   



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

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