[size=xx-large]纯纪念[/size]
[size=x-large]使用typedef后[/size]
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
struct student *next;
};
// 创建一个结点的函数
struct student* createNode()
{
struct student* p = (struct student *)malloc(sizeof(struct student));
p->next = NULL;
return p;
}
// 加结点有两种,一是在头加,一是在尾加
struct student* addHeadNode(struct student *p)
{
struct student *head = createNode();
head->next = p;
return head;
}
//struct student * addTailNode(struct student *p)
// 增加size
struct student* addSizeNode(struct student *head, int size)
{
int i = 0;
for (i=0; i < size; i++)
{
head = addHeadNode(head);
}
return head;
}
// 获取链表大小
int getSize(struct student *p)
{
int size = 0;
while (p != NULL)
{
size++;
p = p->next;
}
return size;
}
// 对结点赋值
void setNode(struct student *p)
{
gets(p->name);
}
// 对整个链表赋值
void setList(struct student *head)
{
while (head != NULL)
{
setNode(head);
head = head->next;
}
}
// 打印链表
void printList(struct student *head)
{
while (head != NULL)
{
printf("name = %s\n", head->name);
head = head->next;
}
}
int main(int argc, char *argv[])
{
// 链表头结点
struct student* head;
head = createNode();
printf("link list size = %d\n", getSize(head));
head = addSizeNode(head, 1);
printf("link list size = %d\n", getSize(head));
setList(head);
printList(head);
return 0;
}
[size=x-large]使用typedef后[/size]
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char name[20];
struct student *next;
} * Student;
// 创建一个结点的函数
Student createNode()
{
Student p = (struct student *)malloc(sizeof(struct student));
p->next = NULL;
return p;
}
// 加结点有两种,一是在头加,一是在尾加
Student addHeadNode(Student p)
{
struct student *head = createNode();
head->next = p;
return head;
}
//struct student * addTailNode(Student p)
// 增加size
Student addSizeNode(Student head, int size)
{
int i = 0;
for (i=0; i < size; i++)
{
head = addHeadNode(head);
}
return head;
}
// 获取链表大小
int getSize(Student p)
{
int size = 0;
while (p != NULL)
{
size++;
p = p->next;
}
return size;
}
// 对结点赋值
void setNode(Student p)
{
gets(p->name);
}
// 对整个链表赋值
void setList(Student head)
{
while (head != NULL)
{
setNode(head);
head = head->next;
}
}
// 打印链表
void printList(Student head)
{
while (head != NULL)
{
printf("name = %s\n", head->name);
head = head->next;
}
}
int main(int argc, char *argv[])
{
// 链表头结点
Student head;
head = createNode();
printf("link list size = %d\n", getSize(head));
head = addSizeNode(head, 1);
printf("link list size = %d\n", getSize(head));
setList(head);
printList(head);
return 0;
}