回 帖 发 新 帖 刷新版面

主题:[讨论]C/C++数据结构中链表遇到的问题 请大侠们多多指教噢

#include<stdio.h>这些是小弟按照课本上的内容模仿的源程序,运行的时候只能建表 
#include<stdlib.h>不知道问题出在哪儿 而且 建表时输入X并不停止循环...郁闷
typedef struct linknode
{
    char data;
    struct linknode *next;
}linnode;
linnode *head;

int n;
void CreateList()
{
    n=0;
    linnode *p,*s;
    char x;
    int z=1;
    head=(linnode *)malloc(sizeof(linnode));
    p=head;
    printf("\n请逐个的输入节点,以“x”为结束标记!\n");
    while(z)
    {
        printf("\n输入一个字符数据,按回车键:");
        scanf("%c",&x);
        getchar();
        
        if(x!='x')
        {
            s=(linnode *)malloc(sizeof(linnode));
            n++;
            s->data=x;
            p->next=s;
            s->next=NULL;
            p=s;
        }
        else z=0;
    }
}
void InsList(int i,char x)
{
    linnode *s,*p;
    p=head;
    int j;
    while(p!=NULL&&j<i)
    {
        j++;
        p=p->next;
    }
    if(p!=NULL)
    {
        s=(linnode *)malloc(sizeof(linnode));
        s->data=x;
        s->next=p->next;
        p->next=s;
        n++;
    }
    else printf("\n线性表为空或者是插入位置超出!\n");
}
void DelList(char x)
{
    linnode *p,*q;
    if(head==NULL)
        printf("\n未创建线性表!\n");
    if(head->next==NULL)
        printf("\n线性表已经为空!\n");
    q=head;
    p=head->next;
    while(p!=NULL&&p->data!=x)
    {
        q=p;
        p=p->next;
    }
    if(p!=NULL)
    {    q->next=p->next;
    free(p);
    printf("\n该节点已经删除\n");
    }
    else printf("\n抱歉!未找到删除的节点\n");
}
void ShowList()
{
    linnode *p=head;
    printf("\n显示线性表里面所有的元素:");
    if(p==NULL||p->next==NULL)
        printf("\n未创建表或者是表为空!\n");
    else 
    {
        printf("\n");
        while(p->next!=NULL)
        {
            printf("%5c",p->next->data);
            p=p->next;
        }
        printf("\n");
    }
}
void SearchList(char x)
{
    linnode *p;
    int i;
    if(head==NULL)
        printf("\n未创建表!\n");
    else if(head->next==NULL)
        printf("\n线性表为空\n");
    p=head->next;
    i=1;
    while(p!=NULL&&p->data!=x)
    {
        p=p->next;
        i++;
    }
    if(p!=NULL)
        printf("\n该节点的位置是%d\n",i);
    else
        printf("抱歉!未找到该结点\n");

}

void main()
{
    head=NULL;
    int choice,i,j=1;
    char x;
    while(j)
    {
        printf("\n");
        printf("\t\t*******************************\n");
        printf("\t\t          1 建表\n");
        printf("\t\t          2 插入\n");
        printf("\t\t          3 删除\n");
        printf("\t\t          4 显示\n");
        printf("\t\t          5 查找\n");
        printf("\t\t          6 表长\n");
        printf("\t\t          0 返回\n");
        printf("\t\t         请输入0----6\n");
        scanf("%d",&choice);
        if(choice==1)
            CreateList();
        else if(choice==2)
        {
            printf("\n请输入要插入的位置i和数据(格式为:i,x)");
            scanf("%d,%c",&i,&x);
            InsList(i,x);
        }
        else if(choice==3)
        {
            printf("请输入要删除的数:");
            scanf("%c",&x);
            DelList(x);
        }
        else if(choice==4)
            ShowList();
        else if(choice==5)
        {
            printf("请输入要查找的数据:");
                scanf("%c",&x);
            SearchList(x);
        }
        else if(choice==6)
        {
            printf("该链表的长度是:%d",n);
        }
        else if(choice==0)
            j=0;
        else 
            printf("输入出错!请重新输入!");
    }
}



    


回复列表 (共1个回复)

沙发


建表时循环不停止    问题出在那个getchar()那里
把那个函数注释掉就可以了,但是输出会有点问题,做点小的修改就可以了

我来回复

您尚未登录,请登录后再回复。点此登录或注册