【数据结构】链表与线性表

Problem A:本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。
input:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0

output:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
利用尾插法来做

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

struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;//链表必备的,记住就行。
};
struct stud_node *head, *tail;//定义首位

void input(){
	struct stud_node *q;
    q=(struct stud_node *)malloc(sizeof(struct stud_node));//申请新的结点
	scanf("%d", &q->num); 
	while(q->num != 0)    
	{
        scanf("%s %d", q->name, &q->score);
		if(head == NULL)       
		{             
			head = q;            
			head->next = NULL;       
		}       
		if(tail != NULL) 
		{           
			tail->next = q;     
		}        
		tail = q; 
		tail->next = NULL;   
		q=(struct stud_node *)malloc(sizeof(struct stud_node));     
		scanf("%d", &q->num);   
	}	
}

int main()
{
    struct stud_node *p;
	
    head = tail = NULL;
    input();
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);

    return 0;
}
发布了71 篇原创文章 · 获赞 5 · 访问量 3410

猜你喜欢

转载自blog.csdn.net/Rainfoo/article/details/102826144