万能的tg,c语言考试中,求帮助,有祭扫

  • o
    omega_du
    /*
    单向链表
    要求:完成Find()函数(位于第95行) */

    #include <stdio.h>
    #include <stdlib.h>

    struct list
    {
    int data;
    struct list *next;
    };
    typedef struct list Sqlist;

    Sqlist* Create(Sqlist*); //创建
    Sqlist* Output(Sqlist*); //输出
    Sqlist* Find(Sqlist*,int); //查找

    int main()
    {
    int n;
    Sqlist *head,*p;
    head = NULL;

    head = Create(head);
    Output(head);

    printf("请输入要查找的结点:");
    scanf("%d",&n);
    getchar();
    p=Find(head,n);
    if (p!=NULL)
    {
    printf("第 %d 个结点的值为:%d\n\n",n,p->data);
    }
    else
    {
    printf("第 %d 个结点未找到!\n\n", n);
    }

    }

    Sqlist* Create(Sqlist* head)
    {
    int i,n;
    Sqlist *q,*s;

    head = (Sqlist*)malloc(sizeof(Sqlist));
    q = head;

    printf("请输入单链表的长度:");
    scanf("%d",&n);
    getchar();
    for(i=0;i<n;i++)
    {
    s=(Sqlist*)malloc(sizeof(Sqlist));
    if(s!=NULL)
    {
    printf("请输入第 %d 个结点的值:",i+1);
    scanf("%d",&s->data);
    getchar();
    q->next=s;
    q=s;
    }
    else
    i--;
    }
    q->next=NULL;
    printf(" 建立链表,操作成功!\n\n");
    return head;
    }

    Sqlist* Output(Sqlist* head)
    {
    Sqlist* q;

    if(head==NULL)
    printf(" 链表为空,操作失败!\n\n");
    else
    {
    q=head->next;
    while(1)
    {
    printf("%d ",q->data);
    if(q->next==NULL)
    break;
    q=q->next;
    }
    printf("\n 输出完毕,操作成功!\n\n");
    }
    return head;
    }

    /*
    查找函数
    传入链及需要查找的节点,返回该节点的地址
    */
    linklist * Find (head,key)
    linklist * head;
    datatype key;
    {
    linklist * p;
    p = head -> next;
    while (p!=NULL)
    if(p->data!=key)
    p-p->next;
    else break;
    rtturn 0;
    }
    Sqlist* Find(Sqlist* head,int n)
    {


    }
  • d
    dejisae
    昨天忘记划卡了。影响考勤。刚刚用C写了个东西。
    不过看了LZ的题目,压力真的好大!
  • q
    qxch
    =_=b,考试还能上网,神了
  • m
    maystrange
    Sqlist* Find(Sqlist* head,int n)
    {
    Sqlist* find = head;
    int nCount = 0;

    if (!head)
    return NULL;

    while (head)
    {
    head = head->next;
    nCount++; // 计算链表长度
    }

    if (n > nCount)
    return NULL;
    else
    {
    while( n > 1)
    {
    find = find->next;
    n--;
    }
    return find;
    }

    }

    随便写了个,希望没错。。。

    [本帖最后由 maystrange 于 2011-5-13 23:05 编辑]
  • r
    redpill
    应该是

    while (head)
    {
    head = head->next;
    nCount++; // 计算链表长度
    }
  • l
    lobydenk
    改一下函数声明,
    Sqlist* Find( Sqlist* head, unsigned int n)
    {
    while( (head != NULL) && (n > 0) )
    {
    --n;
    head = head->next;
    }
    return head;
    }

    [本帖最后由 lobydenk 于 2011-5-13 23:38 编辑]
  • a
    austin17
    看了下回复时间,感觉大家都没帮上LZ的忙……:D
  • m
    maystrange
    眼神真好