数据结构链表自学笔记

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

struct student{
char name[20];
int num;
int score;
};

struct Node {
struct student data;
struct Node * next;
};

struct Node * creatlist(){
struct Node * headnode=(struct Node*)malloc(sizeof(struct Node));
headnode->next=NULL;
return headnode;
}

struct Node creatnode(struct student data){
struct Node
newnode=(struct Node*)malloc(sizeof(struct Node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}

//打印
void printlist(struct Node* headnode)
{
struct Node * pmove;
printf(“name\tnum\tscore\n”);

pmove=headnode->next;
while(pmove)
{
	printf("%s\t%d\t%d\n",pmove->data.name,pmove->data.num,pmove->data.score);
	pmove=pmove->next;

}
printf("\n");

}

//头部插入
void inserthead(struct Node* headnode,struct student data)
{
struct Node *newnode=creatnode( data);
newnode->next=headnode->next;
headnode->next=newnode;
}

//尾部插入
void SListPushBack(struct Node* headnode,struct student data)
{
struct Node node=creatnode( data);
if (headnode== NULL)
{
node->next = headnode;
headnode = node;
return;
}
Node
cur = headnode;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = node;
node->next = NULL;
}

//删除
void deletedata(struct Nodeheadnode,int num)
{
struct Node
pastnode=headnode->next;
struct Node*pastfront=headnode;
if(pastnode==NULL)
printf(“链表为空,无法删除\n”);

while(pastnode->data.num!=num){
	pastfront=pastnode;
	pastnode=pastfront->next;
	if(pastnode==NULL)
		{
		printf("未找到指定位置\n");
		return;
		} 
}
pastfront->next=pastnode->next;
free(pastnode) ;

}

int main(){
struct Node*list=creatlist();
struct student info;
while(1){
printf(“请输入学生的姓名,编号,成绩:”);
setbuf(stdin,NULL);
scanf("%s %d %d",info.name,&info.num,&info.score);
//inserthead(list,info);
SListPushBack(list,info);
printf(“continue y/n?\n”);
setbuf(stdin,NULL);
int choice=getchar();
if(choice==‘n’||choice==‘N’){
break;
}
}
printlist(list);
printf(“请输入要删除学生的编号:”);
scanf("%d",&info.num);
deletedata(list,info.num);
printlist(list);
system(“pause”);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值