| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1233 人关注过本帖
标题:本人是新手小白,刚入编程的门,有个链表问题请教大神
只看楼主 加入收藏
hanlen99
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-9-8
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
本人是新手小白,刚入编程的门,有个链表问题请教大神
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
void menu();
void choose();
char ch;
char a[100];
char b[30];
struct word *head;
struct word *creat(struct word *head);
struct word *delet(struct word *head,char a[100]);
void print(struct word *head);
struct word
{
    char a[100];
    struct word *next;
};
void main ()
{
    system("color 0b");
    menu();
    do
    {
        scanf("%c",&ch);
        system("cls");
        choose();
    }while(ch!='4');
}
void choose()
{
    switch(ch)
    {
    case '1':creat(head);
        break;
    case '2':print(head);
        break;
    case '3':delet(head,a);
        break;
    case '4':printf("\n\n\t感谢您的使用!\n");
        break;
        getch();
    default:menu();
        printf("\n\t\t请重新选择:");
    }
}
void menu()
{
    char *s[5]={"1-输入单词","2-查看单词","3-删除单词","4-退出词汇表","请选择:"};
    int i;
    printf("\t\t----------英语单词学习系统----------");
    printf("\n");
    printf("\t++++++++++++++++++++++++++++++++++++++++++++");
    printf("\n");
    for(i=0;i<5;i++)
    {
        printf("\t\t----------");
        printf("%s",s[i]);
        printf("----------");
        printf("\n");
    }
}
struct word *creat(struct word *head)
{
    struct word *p1,*p2;
    head=p1=p2=NULL;
   
    while(p1->a>0)
    {
        p1=(struct word*)malloc(sizeof(struct word));
        printf("请输入单词,输入0时结束\n");
        scanf("%c",&p1->a);
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
    }
    free(p1);
    p1=NULL;
    p2->next=NULL;
    printf("单词本输入结束");
    return head;
}
void print(struct word *head)
{
    struct word *temp;
    temp=head;
    printf("单词表为:\n");
    while(temp!=NULL)
    {
        printf("%c\n",*temp->a);
        temp=temp->next;
        getch();
    }
}
struct word *delet(struct word *head,char a)
{
    struct word *temp,*p;
    temp=head;
    if (head==NULL)
        printf("\nListis null!\n");
    else
    {
        temp=head;
        while (*temp->a!=a&&temp->next!=NULL)
        {
            p = temp;
            temp = temp->next;
        }
        if(*temp->a==a)
        {
        if(temp==head)
        {
            printf("delete string :%d\n",temp->a);
            head=head->next;
            free(temp);
        }
        else
        {
            p->next=temp->next;
            printf("delete string :%d\n",temp->a);
            free(temp);
        }
    }
    else
    printf("\nno find string!\n");
    }
    return(head);
}




void choose 那对链表的调用有问题吧,但是我不知道怎么改!!!!
搜索更多相关主题的帖子: include system color 
2016-09-08 10:19
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:7 
字符串比较不能用==,应该用strcmp()
2016-09-08 10:28
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
既然用全局变量,函数调用那块就不要传参了。
2016-09-08 10:34
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
void choose 那对链表的调用有问题吧,但是我不知道怎么改!!!!
即然你使用了return.在该函数的调用时可以这样:head=creat(head);
这样头指针就保存下来了
还有如果不想这样,就要用到指针的指针,那个不用返回值。
类似struct word *Head;
Head *head;
这样就可以通过head指针直接改变Head的值。
ps;用全局变量也行。


[此贴子已经被作者于2016-9-8 10:36编辑过]

2016-09-08 10:34
hanlen99
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-9-8
收藏
得分:0 
回复 4楼 ehszt
谢谢你,我尝试一下。。。看PPT学的,感觉老师都没怎么教
2016-09-08 10:36
hanlen99
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-9-8
收藏
得分:0 
回复 3楼 grmmylbs
能具体指一下吗?我才开始学,不是很理解
2016-09-08 10:37
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:7 
[quote]以下是引用hanlen99在2016-9-8 10:19:21的发言:


struct word *head;                //head全局变量   

void choose()
{

    {
    case '1':creat(head);        //这个有几个改法,1、不用形参,直接用全局变量head。2、改成head=creat(head),记得函数返回头指针
        break;
    case '2':print(head);
        break;
   
}
struct word *creat(struct word *head)         //head局部变量
{
    struct word *p1,*p2;
    head=p1=p2=NULL;
   
    while(p1->a>0)                //用这个来作为逻辑判断相当于NUL和EOF两种情况作为跳出循环,所以最好不要这样用
    {
        p1=(struct word*)malloc(sizeof(struct word));
        printf("请输入单词,输入0时结束\n");            //这个字符0是48,和你写的0是两码事
        scanf("%c",&p1->a);
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;        //p2除了赋值NULL后,就没有其它赋值,所以肯定不能这样用,你在纸上画下看看,不会再问。
        p2=p1;
    }

struct word *delet(struct word *head,char a)
{
 
        temp=head;
        while (*temp->a!=a&&temp->next!=NULL)        //先不说字符串能不能这样比较,就想问问你*temp->a!=a和*temp->a!=97有区别吗
        {


我没有测试,如果说的有什么不对不要骂。
2016-09-08 15:26
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void menu();
void choose();
void *creat();
void *delet();
void print();

struct word
{
    char a[100];
    struct word *next;
};

char ch;    //全局变量,后面就不用传递,函数直接调用就好了。
char ah[100];    //结构体里有a,最好区分开来。
char b[30];        //这个变量做什么的没搞懂
struct word *head=NULL;        //这里初始化比在后面初始化好一点

int main ()
{
    system("color 0b");
    struct word *p;
    menu();
    do
    {
        fflush(stdin);
        scanf("%c",&ch);
        system("cls");
        choose();
    }while(ch!='4');
    p=head;
    while(p!=NULL)
    {
        head=head->next;
        free(p);
        p=head;      
    }
    return 0;
}
void choose()
{
    switch(ch)
    {
    case '1':creat();
        break;
    case '2':print();
        break;
    case '3':scanf("%s",ah);delet();
        break;
    case '4':printf("\n\n\t感谢您的使用!\n");
        break;
    default:menu();
        printf("\n\t\t请重新选择:");
    }
}
void menu()
{
    char *s[5]={"1-输入单词","2-查看单词","3-删除单词","4-退出词汇表","请选择:"};
    int i;
    printf("\t\t----------英语单词学习系统----------");
    printf("\n");
    printf("\t++++++++++++++++++++++++++++++++++++++++++++");
    printf("\n");
    for(i=0;i<5;i++)
    {
        printf("\t\t----------");
        printf("%s",s[i]);
        printf("----------");
        printf("\n");
    }
}
void *creat()        //这个随手改的,有兴趣自己将这个函数再修改下
{
    struct word *p1,*p2;
    p1=NULL;
    p2=head;
    char c;
    if(head!=NULL) while(p2->next!=NULL) p2=p2->next;
    p1=(struct word*)malloc(sizeof(struct word));
    printf("请输入单词\n");
    scanf("%s",p1->a);        
    while(1)
    {
        
        if(head==NULL)
           p2=head=p1;
        else
        p2->next=p1;
        p2=p1;
        p2->next=NULL;
        p1=(struct word*)malloc(sizeof(struct word));
        printf("请输入单词\n");
        scanf("%s",p1->a);
        fflush(stdin);
        printf("是否继续输入:N OR Y\n");        
        scanf("%c",&c);
        if(ch=='N'||ch=='n')  break;
    }
    p2->next=p1;
    p1->next=NULL;
    printf("单词本输入结束");
}
void print()
{
    struct word *temp;
    temp=head;
    printf("单词表为:\n");
    while(temp!=NULL)
    {
        printf("%s\n",temp->a);
        temp=temp->next;
    }
}
void *delet()   
{
    struct word *temp,*p;
    p=temp=head;
    if (head==NULL)
        printf("\nListis null!\n");
    else
    {
        while (temp!=NULL)
        {
           if(!strcmp(temp->a,ah))     //只考虑一个,不考虑多个相同,想要多个相同的自己修改
          {
              if(temp==head)
                {
                    printf("delete string :%s\n",temp->a);
                    head=head->next;
                    free(temp);
                }
            else
                {
                       p->next=temp->next;
                    printf("delete string :%s\n",temp->a);
                    free(temp);
                }
                    break;
          }
              p=temp;
            temp=temp->next;
        }
        if(temp==NULL) printf("\nno find string!\n");
    }   
}
2016-09-08 18:05
快速回复:本人是新手小白,刚入编程的门,有个链表问题请教大神
数据加载中...
 
   



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

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