第1个回答 推荐于2017-09-04
//---------------------------------------------------------------------------
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
typedef struct node{
char num;
int score;
struct node *next;
} node;
node *create(int n) /*创建有n个结点的单链表*/
{
node *rt=NULL;
node *now;
node *nt=NULL;
int i;
srand(time(NULL));
for (i = 0,rt=now=malloc(sizeof(node)); i<n; i++) {
if (i) {
nt=malloc(sizeof(node));
nt->next=NULL;
nt->num=rand()%26+'a'; /*对数据域值,此处采用随机数赋值,可以改为从键盘输入*/
nt->score=rand()%101; /*对数据域值,此处采用随机数赋值,可以改为从键盘输入*/
now->next=nt;
now=now->next ;
}
else{
now->num=rand()%26+'a'; /*对数据域值,此处采用随机数赋值,可以改为从键盘输入*/
now->score=rand()%101; /*对数据域值,此处采用随机数赋值,可以改为从键盘输入*/
now->next=NULL;
}
}
return rt;
}
void prt(node *a) /*输出单链表*/
{
while (a)
{
printf("NUM:\t\t%c\nSCORE:\t\t%d\n\n",a->num,a->score);
a=a->next;
}
}
void nfree(node *a) /*释放空间*/
{
node *b;
while (a)
{
b=a->next;
free(a);
a=b;
}
}
int main(void)
{
node *na=create(3); /*调用create()创建包含3个结点的单链表*/
prt(na);
nfree(na);
return 0;
}
//---------------------------------------------------------------------------本回答被提问者采纳
第2个回答 2018-02-28
#include<stdio.h>
#include<stdlib.h>
//链表结构类型
typedef struct Node{
char num;
int score;
Node* next;
}*List;
int main(){
List L;
//创建三个结点
Node* l1=(Node*)malloc(sizeof(Node));
Node* l2=(Node*)malloc(sizeof(Node));
Node* l3=(Node*)malloc(sizeof(Node));
//修改三个结点信息
l1->num='1';
l1->score=100;
l2->num='2';
l2->score=85;
l3->num='3';
l3->score=60;
//连接关系
L=l1;
l1->next=l2;
l2->next=l3;
l3->next=NULL;
//循环输出链表信息
Node* p=L;
while(p!=NULL){
printf("num:%c\n",p->num);
printf("score:%d\n\n",p->score);
p=p->next;
}
//删除链表
free(l1);
free(l2);
free(l3);
return 0;
}
第3个回答 2016-02-03
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MY_MALLOC_TYPEDEF(td) (td *)calloc(1, sizeof(td))
//定义一个结构体
typedef struct node
{
int age;
char name[64];
struct node *next;
}STU;
STU *head, *tail;
STU *createEntry(int age, char *name)
{
STU *entry;
entry = MY_MALLOC_TYPEDEF(STU);
if(NULL == entry)
{
return NULL;
}
entry->age = age;
strcpy(entry->name, name);
//尾插法
if(NULL == head)
{
head = tail = entry;
return entry;
}
tail->next = entry;
tail = tail->next;
return entry;
}
void showList(STU *list)
{
if(NULL == list)
{
printf("List is empty!\n");
return;
}
while(list)
{
printf("%s/%d ", list->name, list->age);
list = list->next;
}
printf("\n");
}
int main(int argc, char **argv)
{
STU *p, *q, *t;
//创建3个节点
q = createEntry(18, "persona");
p = createEntry(19, "personb");
t = createEntry(20, "personc");
showList(head);
return 0;
}
第4个回答 2009-12-14
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student //定义一个结构体student
{
char num[10];
int score;
struct student *next;//指向下个结点的指针
};
struct student *head;//定义一个链表头指针
void main()
{
int i;
struct student *p;
for(i=1;i<4;i++)
{
p=(struct student *)malloc(LEN);
printf("\n请输入学号和成绩:");
scanf("%s%d",p->num,&(p->score));
if(i==1)
{
head=p;
head->next=NULL;
}
else if(i==2)
{
head->next=p;
p->next=NULL;
}
else
{
(head->next)->next=p;
p->next=NULL;
}
}
p=head;
while(p!=NULL)
{
printf("\n%10s%5d\n",p->num,p->score);//输出当前结点
p=p->next;//p指向下个结点
}
}