标题:求解为什么不行哇
只看楼主
A928290513
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-9-24
 问题点数:0 回复次数:1 
求解为什么不行哇
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define Linkedlist struct sl
Linkedlist
{
    int data;
    Linkedlist *next;
};
void Create_Linkedlist(Linkedlist *h)
{
    int i,x,n;
    Linkedlist *p,*s;
    printf("要存入几个数值?\n");fflush(stdout);
    p=h;
    p=(Linkedlist*)malloc(sizeof(Linkedlist));
    s=(Linkedlist*)malloc(sizeof(Linkedlist));
    scanf("%d",&n);
    printf("请输入:");
    for(i=1;i<=n;i++)
    {
        scanf("%d\n",&x);
        s->data=x;
        if(h->next==NULL)
            h->next=s;
        else
            p->next=s;
        p=s;
    }fflush(stdout);
    printf("已经存储成功");
    p->next=NULL;
}
Linkedlist *Insert_Linkedlist(Linkedlist *h)
{
    int i,j,x;
    Linkedlist *p,*s;
    s=(Linkedlist*)malloc(sizeof(Linkedlist));
    p=(Linkedlist*)malloc(sizeof(Linkedlist));
    p=h;
    printf("请输入要插入的位置\n");
    scanf("%d",&i);
    printf("请输入要插入的数值\n");
    scanf("%d",&x);
    for(j=1;j<i;j++)
        p=p->next;
    s->data=x;
    s->next=p->next;
    p->next=s;
    return h;
}
Linkedlist * Delete_Linkedlist(Linkedlist *h)
{
    int i,j;
    Linkedlist *p;
    p=(Linkedlist*)malloc(sizeof(Linkedlist));
    p=h;
    printf("请输入要删除的位置\n");
    scanf("%d",&i);
    for(j=1;j<i;j++)
        p=p->next;
    p->next=(p->next)->next;
    return h;
}
void Printf_Linkedlist(Linkedlist *h)
{
    int i=1;
    Linkedlist *p;
    p=(Linkedlist*)malloc(sizeof(Linkedlist));
    p=h->next;
    for(p=h->next;(p->next)!=NULL;p=p->next)
        printf("a[%d]=%d",i++,p->data);
}
int menu_select()
{
    char c;
    do
    {
        
        printf("请选择操作:\n1:创建单链表\n2:插入数据\n3:删除数据\n4:输出数据\n5:退出\n");
        c=getchar();
    }while(c<'1'||c>'5');
    return (c-'0');
}
main()
{
    Linkedlist *H;
    H=(Linkedlist*)malloc(sizeof(Linkedlist));
    H->next=NULL;
    for( ;; )
    {
        
        switch(menu_select())
        {
        case 1:
             Create_Linkedlist(H);break;
        case 2:
            Insert_Linkedlist(H);break;
        case 3:
            Delete_Linkedlist(H);break;
        case 4:
            Printf_Linkedlist(H);break;
        case 5:
            printf("Goodbye");exit(0);
        }
        system("cls");
    }
}
搜索更多相关主题的帖子: int next printf malloc sizeof 
2017-09-24 14:43
A928290513
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-9-24
得分:0 
首先 输入和输出的位置不按照代码的顺序显示
然后 插入 删除和显示操作不行,容易崩溃
2017-09-24 14:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-480907-1-1.html




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

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