打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
054.链表操作(1)
#include <stdio.h>
#include <conio.h>
#define N 10

typedef struct node
{
    char name[20];
    struct node *link;
}stud;

stud * creat()
{
    stud *p,*h,*s;
    int i,n;
    puts("\nPlease input the number of linklist:");
    scanf("%d",&n);
    if((h=(stud *)malloc(sizeof(stud)))==NULL)
    {
        printf("cannot find space!");
        exit(0);
    }
    h->name[0]='\0';
    h->link=NULL;
    p=h;
    for(i=0;i<n;i++)
    {
        if((s= (stud *) malloc(sizeof(stud)))==NULL)
        {
            printf("cannot find space!");
            exit(0);
        }
        p->link=s;
    printf("please input %d student's name: ",i+1);
        scanf("%s",s->name);
        s->link=NULL;
        p=s;
    }

    return(h);
}

stud * search(stud *h,char *x)
{
    stud *p;
    char *y;
    p=h->link;
    while(p!=NULL)
    {
        y=p->name;
        if(strcmp(y,x)==0)
        return(p);
        else p=p->link;
    }
    if(p==NULL)
    printf("data not find!");
	return 0;
}

stud * search2(stud *h,char *x)
{
    stud *p,*s;
    char *y;
    p=h->link;
    s=h;
    while(p!=NULL)
    {
        y=p->name;
        if(strcmp(y,x)==0)
            return(s);
        else
        {
            p=p->link;
            s=s->link;
        }
    }
    if(p==NULL)
    printf("data not find!");
	return 0;
}

void insert(stud *p)
{
    char stuname[20];
    stud *s;
    if((s= (stud *) malloc(sizeof(stud)))==NULL)
    {
        printf("cannot find space!");
        exit(0);
    }
printf("\nplease input the student's name: ");
    scanf("%s",stuname);
    strcpy(s->name,stuname);
    s->link=p->link;
    p->link=s;
}

void del(stud *x,stud *y)
{
    stud *s;
    s=y;
x->link=y->link;
    free(s);
}

void print(stud *h)
{
    stud *p;
    p=h->link;
printf("Now the link list is:\n");
    while(p!=NULL)
    {
        printf("%s ",&*(p->name));
        p=p->link;
}
printf("\n");
}

void quit()
{
clrscr();
puts("\n Thank you for your using!\n Press any key to quit...");
getch();
exit(0);
}

void menu(void)
{
    clrscr();
printf("       simple linklise realization of c\n");
printf("    ||=====================================||\n");
printf("    ||                                     ||\n");
printf("    || [1]  create linklist                ||\n");
printf("    || [2]  seach                          ||\n");
printf("    || [3]  insert                         ||\n");
printf("    || [4]  delete                         ||\n");
printf("    || [5]  print                          ||\n");
printf("    || [6]  exit                           ||\n");
printf("    ||                                     ||\n");
printf("    || if no list exist,create first       ||\n");
printf("    ||                                     ||\n");
printf("    ||=====================================||\n");
printf("      Please input your choose(1-6): ");
}

main()
{
    int choose;
    stud *head,*searchpoint,*forepoint;
    char fullname[20];


    while(1)
    {
        menu();
        scanf("%d",&choose);
        switch(choose)
        {
            case 1:
            	clrscr();
            	head=creat();
            	puts("Linklist created successfully! \nPress any key to return...");
            	getch();
            break;
            case 2:
            	clrscr();
                printf("Input the student's name which you want to find:\n");
                scanf("%s",fullname);
                searchpoint=search(head,fullname);
                printf("The stud name you want to find is:%s",*&searchpoint->name);
                printf("\nPress any key to returen...");
	    		getchar();
	    		getchar();
                break;
            case 3:
                clrscr();
                insert(head);
                print(head);
                printf("\nPress any key to returen...");
                getchar();getchar();
                break;
            case 4:
                clrscr();
                print(head);
                printf("\nInput the student's name which you want to delete:\n");
                scanf("%s",fullname);
                searchpoint=search(head,fullname);
                forepoint=search2(head,fullname);
                del(forepoint,searchpoint);
                print(head);
	    puts("\nDelete successfully! Press any key to return...");
	    getchar();
	    getchar();
                break;
            case 5:print(head);
                printf("\nPress any key to return...");
                getchar();getchar();
                break;
            case 6:quit();
                break;
            default:
                clrscr();
                printf("Illegal letter! Press any key to return...");
                menu();
                getchar();
        }
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【转】C语言 链表操作
编程中国
学生成绩管理系统(C语言)
C语言 详解华为笔试试题 租借网球场地
经典程序100例(91-100)
链表的简单创建
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服