带有头结点的链表,插入和删除时不需要区分插入删除的位置(链头,链中尾)
struct Student{
int num;
char name[20];
double score[3];
double average;
struct Student * next;
};
struct Student *head,*n,*p;
head=(struct Student *)malloc(sizeof(struct Student));
head->num=101//不能在头结点中存放数据
第一个结点
n=(struct Student *)malloc(sizeof(struct Student));
n->num=101;
n->next=NULL;
…为第一个结点的数据域赋值
建立链
head->next=n
p=n;
第二个结点
n=(struct Student *)malloc(sizeof(struct Student));
n->num=101;
n->next=NULL;
…为第一个结点的数据域赋值
建立链
p->next=n;
p=n;
第三个结点
n=(struct Student *)malloc(sizeof(struct Student));
n->num=101;
n->next=NULL;
…为第一个结点的数据域赋值
建立链
p->next=n;
p=n;
struct Student{
int num;
char name[20];
double score[3];
double average;
struct Student * next;};
int main()
{
// struct Student *p;
struct Student *head;
// struct Student *n;
head=(struct Student *)malloc(sizeof(struct Student));
/*p=head;
for(i=0;i<3;i++)
{
n=head=(struct Student )malloc(sizeof(struct Student));
n->num=101+i;
n->next=NULL;
p->next=n;
p=n;
}/
create(head);
}
void create(struct Student *p)
{
int i;
struct Student *n;
for(i=0;i<3;i++)
{
n=head=(struct Student *)malloc(sizeof(struct Student));
n->num=101+i;
n->next=NULL;
p->next=n;
p=n;
}
}
void del(struct Student *p,int i)
{
int j=0;
while(j<i)
{
p=p->next;
}
p->next=p->next->next;
}
用typedef声明别名(小名)
typedef struct Student{
int num;
char name[20];
double score[3];
double average;
} ElementType;
// typedef char ElmentType;
struct Node{
ElmentType data;//struct Student data;
struct Node *next;
}
数据结构=数据的逻辑结构(一对一,一对多,多对多)+数据的存储结构(存储数据元素自身+数据之间的关系,有顺序存储结构和链式存储结构)+算法