#include<stdio.h>
#include<stdlib.h>
typedef struct girl{
char name[20];
int classnumber;
int number;
}girl;
typedef struct stu{
girl data;
struct stu* next;
}stu;
//创建链表
stucreatlink(){
stu headlink=(stu*)malloc(sizeof(stu));
headlink->next =NULL;
return headlink;
}
//创建链接
stu creatnewnode(girl data){
stu newnode=(stu*)malloc(sizeof(stu));
newnode->data =data;
newnode->next =NULL;
return newnode;
}
//从头插入链表
void insert(stuheadlink,girl data){
stu newnode =creatnewnode(data);
newnode->next =headlink->next ;
headlink->next =newnode;
}
//打印链表
void printlink(stu* headlink){
stu* p=headlink->next ;
while(p->next !=NULL){
printf("%s\t%d\t%d\n",p->data.name ,p->data.classnumber ,p->data.number );
p=p->next ;
}
}
//搜索连节
/struct/
void seeklink(stu *headlink,char name){
stu p=headlink->next ;
while(p->data.name !=name){
p=p->next ;
}
printf("%s\t%d\t%d\n",p->data.name ,p->data.classnumber ,p->data.number );
}
//删除链表
void deletelink(stu* headlink, girl data ){
stu* p=headlink;
stu* beforep=headlink->next;
while(p!=NULL){
if(p->data.name !=data.name )
beforep=p;
p=p->next ;
break;
}
beforep->next =p->next ;
free(p);
}
int main (){
int i=0;
girl g;
stu*head=creatlink();
for(i=0;i<5;i++){
scanf("%s%d%d",g.name ,g.classnumber ,g.number );
insert(head,g);
}
printlink(head);
return 0;
}
修改如下,供参考:
#include<stdio.h>
#include<stdlib.h>
typedef struct girl {
char name[20];
int classnumber;
int number;
}girl;
typedef struct stu {
girl data;
struct stu* next;
}stu;
//创建链表
stu *creatlink() {
stu *headlink = (stu*)malloc(sizeof(stu));
headlink->next = NULL;
return headlink;
}
//创建链接
stu *creatnewnode(girl data) {
stu *newnode = (stu*)malloc(sizeof(stu));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
//从头插入链表
void insert(stu *headlink, girl data) {
stu *newnode = creatnewnode(data);
newnode->next = headlink->next;
headlink->next = newnode;
}
//打印链表
void printlink(stu* headlink) {
stu* p = headlink->next;
while (p != NULL) { //(p->next != NULL)
printf("%s\t%d\t%d\n", p->data.name, p->data.classnumber, p->data.number);
p = p->next;
}
}
//搜索连节
//struct /
void seeklink(stu * headlink, char *name) {
stu *p = headlink->next;
while (p && strcmp(p->data.name, name) != 0) { //while (p->data.name != name)
p = p->next;
}
if (p != NULL)
printf("\n%s\t%d\t%d\n", p->data.name, p->data.classnumber, p->data.number);
else
printf("not found!\n");
}
//删除链表
void deletelink(stu* headlink, char* name) {
stu* p = headlink->next;
stu* beforep = headlink;
while (p != NULL) {
if (strcmp(p->data.name, name) != 0) { //if (p->data.name != data.name)
beforep = p;
p = p->next;
}
else
break;
}
if (p != NULL) {
beforep->next = p->next;
free(p);
}
else
printf("not found!\n");
}
int main() {
int i = 0;
girl g;
stu* head = creatlink();
for (i = 0; i < 5; i++) {
scanf("%s%d%d", g.name, &g.classnumber, &g.number); //scanf("%s%d%d", g.name, g.classnumber, g.number);
insert(head, g);
}
printlink(head);
return 0;
}
删除链表:1. 现在删除逻辑是不等于时删除 2.有个问题,当p=null时,p->next 是有问题的
打印链表: 这样试试
void printlink(stu* headlink){
stu* p=headlink;
while(p !=NULL){
printf("%s\t%d\t%d\n",p->data.name ,p->data.classnumber ,p->data.number );
p=p->next ;
}
}