小百求助,关于简单链表的建立,求大神解答,谢谢!

AXDii 2017-03-13 11:32:34
小白编写了如下链表:

#include<iostream>
using namespace std;

typedef struct Node List;
typedef List * PrtToNode;
typedef int ElementType;

struct Node{
ElementType Element;
PrtToNode next;
};


PrtToNode MakeList(PrtToNode head);
void ShowList(PrtToNode head);

PrtToNode Intersection(PrtToNode p1, PrtToNode p2);
PrtToNode Union(PrtToNode p1, PrtToNode p2);

int main()
{
PrtToNode head;
head = MakeList(head);
ShowList(head);
return 0;
}

PrtToNode MakeList(PrtToNode head)
{
PrtToNode ps;
PrtToNode pend;

ps = (PrtToNode)malloc(sizeof(List)); //在for循环里要输入六个数才能结束输入,而小白只是循环了5次
pend = ps;
for(int i = 0; i < 5; i++){
scanf("%d ", &(ps ->Element));
if (head == NULL)
head = ps;
else
pend ->next = ps;
pend = ps;
ps = (PrtToNode)malloc(sizeof(List));
}
pend ->next = NULL;
pend = pend->next;

free(ps);
free(pend);
return head;
}

void ShowList(PrtToNode head)
{
PrtToNode ps = head;
while(ps){
printf("%d ", ps ->Element);
ps = ps->next;
}
free(ps);
}


//在for循环里要输入六个数才能结束输入,而小白只是循环了5次,为什么会要输入六次,求助,谢谢。
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-03-14
  • 打赏
  • 举报
回复
仅供参考:
//带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
    int          data;
    struct NODE *next;
} H,*head,*p,*q,*q1,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t,m;
int main() {
    srand(time(NULL));

    //填写头节点数据
    H.data=-1;
    H.next=NULL;
    head=&H;

    //创建10个节点的单链表
    p=head;
    for (i=0;i<10;i++) {
        q=(struct NODE *)malloc(sizeof(struct NODE));
        if (NULL==q) return 1;
        q->data=rand()%100;//填写0..99的随机值
        q->next=NULL;
        p->next=q;
        p=q;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将值为5的结点插入到单链表的第k个结点前
    k=3;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=(struct NODE *)malloc(sizeof(struct NODE));
            if (NULL==q) return 1;
            q->data=5;
            q->next=p->next;
            p->next=q;
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //删除第k个节点
    k=5;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=p->next;
            if (q) {
                p->next=q->next;
                free(q);
            }
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //从小到大排序
    for (p=head;p!=NULL && p->next!=NULL;p=p->next) {
        for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {
            if (p->next->data > q->next->data) {

                //交换data
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
//              t=p->next->data;p->next->data=q->next->data;q->next->data=t;

                //或者

                //交换next
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
                s1=p->next;
                s2=p->next->next;
                s3=q->next;
                s4=q->next->next;

                if (s2!=s3) {
                     p->next=s3;
                    s3->next=s2;
                     q->next=s1;
                    s1->next=s4;
                } else {
                     p->next=s3;
                    s3->next=s1;
                           q=s3;
                    s1->next=s4;
                }

                //输出整个单链表
//              s=head->next;
//              while (1) {
//                  if (NULL==s) {
//                      printf("\n");
//                      break;
//                  }
//                  printf("%02d->",s->data);
//                  s=s->next;
//              }
//              getchar();
            }
        }
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将整个链表逆序
    if (head->next!=NULL && head->next->next!=NULL) {
        p=head->next;
        q=p->next;
        p->next=NULL;
        while (1) {
            q1=q->next;
            q->next=p;
            p=q;
            q=q1;
            if (NULL==q) break;
        }
        head->next=p;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10
    m=4;
    n=6;
    k=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        k++;
        if (m+1==k) {
            q=p;
        }
        s=p;
        p=p->next;
    }
    s1=head->next;
    head->next=q->next;
    s->next=s1;
    q->next=NULL;

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //释放所有节点
    p=head->next;
    while (1) {
        if (NULL==p) {
            break;
        }
        q=p->next;
        free(p);
        p=q;
    }

    return 0;
}
//84->28->20->23->96->19->59->97->29->41->
//84->28->05->20->23->96->19->59->97->29->41->
//84->28->05->20->96->19->59->97->29->41->
//05->19->20->28->29->41->59->84->96->97->
//97->96->84->59->41->29->28->20->19->05->
//41->29->28->20->19->05->97->96->84->59->
//
paschen 2017-03-14
  • 打赏
  • 举报
回复
scanf("%d ", &(ps ->Element)); 改成: scanf("%d", &(ps ->Element)); //没那个空格 否则你输入一个数后,它是不会立即显示的,要等再接收到一个非(空格、制表符、回车)的输入scanf语句才结束。
自信男孩 2017-03-14
  • 打赏
  • 举报
回复

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

typedef struct Node List;
typedef List *PrtToNode;
typedef int ElementType;

struct Node{
    ElementType Element;
    PrtToNode next;
};


PrtToNode MakeList(PrtToNode head);
void ShowList(PrtToNode head);

PrtToNode Intersection(PrtToNode p1, PrtToNode p2);
PrtToNode Union(PrtToNode p1, PrtToNode p2);

int main()
{
    PrtToNode head = NULL;    /* must initize */
    head = MakeList(head);
    ShowList(head);
    return 0;
}

PrtToNode MakeList(PrtToNode head)
{
    int i;
    PrtToNode ps;
    PrtToNode pend;

    ps = (PrtToNode)malloc(sizeof(List));
    pend = ps;
    for(i = 0; i < 5; i++){
        scanf("%d", &(ps ->Element));
        if (!head)    /* use main's initization*/
            head = ps;
        else
            pend ->next = ps;
        pend = ps;
        ps = (PrtToNode)malloc(sizeof(List));
    }
    pend ->next = NULL;
    pend = pend->next;

    free(ps);
    //free(pend);
    return head;
}

void ShowList(PrtToNode head)
{
    PrtToNode ps = head;
    while(ps){
        printf("%d ", ps ->Element);
        ps = ps->next;
    }
    //free(ps);  /* del this statement , ps is NULL*/
}

1. 循环次数的不对,可能是输入scanf中多的空格导致的。 2. main的head需要初始化,不然在MakeList中找不到head == NULL,因为不初始化,head就是一个野指针。其实这个函数不需要这个参数,完全可以去掉,在MakeList中定义一个头指针,然后初始化一下。最后返回头指针。 另外,ps和pend不要free,pend已经是NULL,free没有意义。ps需要free掉,因为你多申请了一个ps。(循环5次,申请5次,外面还有一次). 3. show函数里一样,最后的ps不用释放,已经是NULL,释放也是没意义。 4。对于要释放整个链表,建议重新定义一个函数去做。
幻夢之葉 2017-03-14
  • 打赏
  • 举报
回复
scanf("%d ", &(ps->Element)); 改为 scanf("%d", &(ps->Element)); //“%d后面不要空格 int main() { //PrtToNode head; PrtToNode head = NULL; head = MakeList(head); ShowList(head); return 0; }
AXDii 2017-03-14
  • 打赏
  • 举报
回复
引用 3 楼 cfjtaishan 的回复:

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

typedef struct Node List;
typedef List *PrtToNode;
typedef int ElementType;

struct Node{
    ElementType Element;
    PrtToNode next;
};


PrtToNode MakeList(PrtToNode head);
void ShowList(PrtToNode head);

PrtToNode Intersection(PrtToNode p1, PrtToNode p2);
PrtToNode Union(PrtToNode p1, PrtToNode p2);

int main()
{
    PrtToNode head = NULL;    /* must initize */
    head = MakeList(head);
    ShowList(head);
    return 0;
}

PrtToNode MakeList(PrtToNode head)
{
    int i;
    PrtToNode ps;
    PrtToNode pend;

    ps = (PrtToNode)malloc(sizeof(List));
    pend = ps;
    for(i = 0; i < 5; i++){
        scanf("%d", &(ps ->Element));
        if (!head)    /* use main's initization*/
            head = ps;
        else
            pend ->next = ps;
        pend = ps;
        ps = (PrtToNode)malloc(sizeof(List));
    }
    pend ->next = NULL;
    pend = pend->next;

    free(ps);
    //free(pend);
    return head;
}

void ShowList(PrtToNode head)
{
    PrtToNode ps = head;
    while(ps){
        printf("%d ", ps ->Element);
        ps = ps->next;
    }
    //free(ps);  /* del this statement , ps is NULL*/
}

1. 循环次数的不对,可能是输入scanf中多的空格导致的。 2. main的head需要初始化,不然在MakeList中找不到head == NULL,因为不初始化,head就是一个野指针。其实这个函数不需要这个参数,完全可以去掉,在MakeList中定义一个头指针,然后初始化一下。最后返回头指针。 另外,ps和pend不要free,pend已经是NULL,free没有意义。ps需要free掉,因为你多申请了一个ps。(循环5次,申请5次,外面还有一次). 3. show函数里一样,最后的ps不用释放,已经是NULL,释放也是没意义。 4。对于要释放整个链表,建议重新定义一个函数去做。
非常感谢,问题解决了
ck2333 2017-03-13
  • 打赏
  • 举报
回复
弄不清楚哪里错了,就重新整理下思路,然后一步一步的看。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧