单双链表代码

来源:互联网 发布:visual studio写java 编辑:程序博客网 时间:2024/06/07 11:37
#include<stdio.h>
#include<stdlib.h>


typedef struct student_info
{
        char stu_name[20];
        int stu_score;
        int index;
        struct student_info *prei;
        struct student_info *next;
} stu_info;


static stu_info *stu_info_list_create(void)
{
        int n = 0;
        stu_info *head,*tail,*new;


        new = (stu_info*)malloc(sizeof(stu_info));
        if(new == NULL) return;
        printf("\ninut the  name:");
        scanf("%s",new->stu_name);
        printf("\ninut the score:");
        scanf("%d",&new->stu_score);
        new->index = 1;
        new->next = NULL;

        new->prei = NULL;

        //while(new->stu_score >= 0)
        //while((strcmp(new->stu_name,"q")==0)||(strcmp(new->stu_name,"Q")==0))
        while(1)
        {
                n = n+1;
                if(n == 1)
                {
                        head = new;
                        tail = head;
                }


                new = (stu_info*)malloc(sizeof(stu_info));
                if(new == NULL) return;


                printf("\ninut the  name:");
                scanf("%20s",new->stu_name);
                if((strcmp(new->stu_name,"q")==0)||(strcmp(new->stu_name,"Q")==0))
                break;
                printf("\ninut the score:");
                scanf("%d",&new->stu_score);
                new->index = n;
                new->prei = NULL;
                new->next = NULL;


                tail->next = new;
                new->prei = tail;
                tail = new;


        }

        return head;
}
static void stu_info_list_show_index(stu_info *head,int index)
{
        stu_info *p = head;
        while(p->index != index)
        {
                p = p->next;
        }
        printf("\n%d,%s,%d",p->index,p->stu_name,p->stu_score);
        p = p->prei;
        printf("\n%d,%s,%d",p->index,p->stu_name,p->stu_score);
}
static void stu_info_list_show(stu_info *head)
{
        stu_info *p = head;
        while(p != NULL)
        {
                printf("\n%d name:%-10s score:%d",p->index,p->stu_name,p->stu_score);
                p = p->next;
        }
        printf("\n");
        return;
}

static void destroy_list(stu_info *head)
{
        stu_info *p = head;
        stu_info *q;
        while(p != NULL)
        {
                q = p->next;
                free(p);
                p->next = NULL;
                p = q;
        }
        free(head);
        head = NULL;
        return;
}
static void write_stu_info_to_file(stu_info* head)
{
        FILE *fp;
        stu_info *p = head;


        fp = fopen("stu_info.txt","w+");
        fputs("the info of student:\n",fp);
        while(p != NULL)
        {
                fprintf(fp,"%5d,%-10s,%3d\n",p->index,p->stu_name,p->stu_score);
                p = p->next;
        }
        fclose(fp);
        return;
}
void main()
{
        stu_info *head;


        /* create the list for stu_info */
        head = stu_info_list_create();
        if(head == NULL) return;
        //show the stu_info have input
        stu_info_list_show(head);


        /* write the stu_info to the file */
        write_stu_info_to_file(head);


        stu_info_list_show_index(head,4);
        destroy_list(head);


}


3 0