C语言基础学习(15)结构链表

结构数组

/**   filmls.c  -- 使用一个结构数组    */
#include<stdio.h>
#include<string.h>
#define TSIZE 45  //储存片名数组大小
#define FMAX 2    //影片最大数量


struct film{
    char title[TSIZE];
    int rating;
};
char * s_gets(char str[], int);

int main(void)
{
    struct film movies[TSIZE];
    int i = 0;
    int j;
    puts("Enter first movie title");
    while(i < FMAX && s_gets(movies[i].title, TSIZE) != NULL
          && movies[i].title[0] != '\0'){
            puts("Enter your rating <0-10>:");
            scanf("%d", &movies[i++].rating);
            printf("%d\n", i);
            while(getchar()!= '\n')
                continue;

            puts("Enter next movie title(or empty line to stop):");

          }
        if( i == 0)
            printf("No data enter. ");
        else

            printf("Here is the movie list: \n");
          for(j = 0; j < i; j++)
            printf("Movie: %s Rating: %d\n", movies[j].title, movies[j].rating);
          printf("Bye !\n");


	return 0;
 }

char * s_gets(char * st, int n)
{
    char * ret_val;
    int i = 0;
    ret_val = fgets(st, n, stdin);
    //printf(("%c  \n", ret_val));
    if(ret_val)
    {
        while(st[i] != '\n' && st[i] != '\0')
            i++;
        if(st[i] == '\n')
            st[i] = '\0';
        else
            while(getchar() != '\n')
            continue;


    }
    return ret_val;
}

结构链表

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char * s_gets(char*, int);
struct file
{
    char title[10];
    int rating;
    char * next;


};

int main()
{

    struct file * head = NULL;
    struct file *prev, * current;
    char input[10];

    /**  储存数组 */
    puts("Enter a movie name:");
    while(s_gets(input, 10) != NULL && input[0] != '\0')
    {
        current = (struct file *) malloc(sizeof(struct file));
        if(head == NULL)
            head = current;
        else
        prev->next = current;
        current->next = NULL;
        strcpy(current->title, input);
        puts("Enter your rating:");
        scanf("%d", &current->rating);
        while(getchar() != '\n')
            continue;
        puts("Input the next movie name:");
        prev = current;
    }


    /**  显示电影列表    */
    if(head == NULL)
        printf("No data entered!\n");
     else
        printf("There is a movies list: \n");
        current = head;
     while(current != NULL)
        {
            printf("Movie : %s  Rating: %d \n",
            current->title, current->rating);
            current = current->next;

        }
/** 释放内存  */
		current = head;
        while(current != NULL)
        {
            current = head;
            head = current->next;
            free(current);        
            }
    puts("Bye !");

    return 0;
}
char * s_gets( char* st, int n)
{
    char * rel_val;
    int i = 0;
    //puts("Enter your string");
    rel_val = fgets(st, n, stdin);
    if(rel_val)
    {
      while(st[i] != '\0' && st[i] != '\n')
        i++;
      if(st[i] == '\n')
        st[i] = '\0';
      else
        while(getchar() != '\n')
        continue;

    }
    //printf("rel_val:%s\n", rel_val);
    return rel_val;
}

char* head;赋值给current时指针类型不一样
应该把head也改成struct file *类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值