当前位置:编程学习 > C/C++ >>

C语言、数据结构。

问题:请写出双向链表的插入、删除、查找函数。

 

此题考试题目,要求答案100%准确,简洁。最后附上解释最好,谢谢。

追问:能不能写简洁点的,插入,删除,查找,分别加注释,写的越简单越好,分数不是问题。
答案:方案一

#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("       易做图 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

上一个:C语言课程设计任务书
下一个:c语言怎么才能入门

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,