关于链表 C【2018.3.20】

本文提供了一个使用C语言实现链表的基本示例,包括创建、遍历、插入和删除节点等功能,并对链表操作进行了详细说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于链表与结构体:

#include<stdio.h>
#include<stdlib.h>
struct stu
{
	struct stu *p,*q,*head,*rear;
	int num;
	int score;
	struct stu *next;
};
struct stu *create()			//建立链表 
{
	struct stu *p,*q,*head,*rear;
	head=rear=NULL;
	int l,n,m;
while(1){
	printf("请输入学号:");
	scanf("%d",&n);
	if(n<0) break;
	else{
		p=(struct stu*)malloc(sizeof(struct stu));
		p->num=n;
	printf("请输入成绩:");
	scanf("%d",&p->score);
	if(rear==NULL){
		head=p;
		p->next=NULL;
		rear=p;
	}else {
		p->next=rear->next;
		rear->next=p;
		rear=p;
	}
}
}
return head;
}
void output (struct stu *head){				//	遍历链表 
	struct stu *p;
	p=head;
	while(p!=NULL){
		printf("\nnum=%d\tscore=%d",p->num,p->score);
		p=p->next;
	}
}
struct stu *insert(struct stu *head,struct stu *pTemp)		//插入链表 
{
	struct stu *p,*q;
	q=NULL;p=head;
	while(p!=NULL&&pTemp->num>p->num){
		q=p;
		p=p->next;
	}
	if(p==head)
	{
		pTemp->next=p;
		head=pTemp;
	}
	else{
		pTemp->next=p;
		q->next=pTemp;
	}
	return head;
}
struct stu *deleted(struct stu *head,int n)		//删除链表结点 
{
	struct stu *p,*q;
	q=NULL;p=head;
	while(n!=p->num&&p->next=NULL){
		q=p;
		p=p->next;
	}
	if(n==p->num){
	if(p==head){
		head=p->next;
		free(p);
	}else{
		q->next=p->next;
		free(p);
	}
}else printf("\nIt's not found");
	return head;
}
int main()
{
	struct stu *head,*p,*q;
	head=create();									//建立链表 
	output(head);									//输出链表 
	p=(struct stu*)malloc(sizeof(struct stu));		 
	scanf("%d",p->num);
	scanf("%d",p->score);
	head=insert(head,p);							//插入结点 
	scanf("%d",n)
	deleted(head,n);								//删除结点 
	output(head);
	return 0;
}

总结:

1、注意两个函数:gets()与puts(),前者读取一行字符串,空格不能停止,碰见回车才停止。后者正常输出,但后面自动回车。

2、以后浮点数输入输出尽量用double,回避float。

在C语言中,如果我们要使用链表来存储图书信息,首先需要定义链表节点结构,并将图书信息作为成员变量。接着,我们创建三个节点实例,并链接它们形成一个简单的单向链表。这里是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义链表节点结构 typedef struct Node { char title[50]; char author[50]; int year; struct Node* next; } Node; // 动态分配内存并初始化新节点 Node* createNode(char* title, char* author, int year) { Node* newNode = malloc(sizeof(Node)); if (newNode) { strcpy(newNode->title, title); strcpy(newNode->author, author); newNode->year = year; newNode->next = NULL; } return newNode; } // 添加节点到链表 void appendToLinkedList(Node** head, Node* newNode) { if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } // 遍历链表并输出图书信息 void printLinkedList(Node* head) { Node* temp = head; while (temp != NULL) { printf("第%zu本书:\n", temp - head + 1); // 使用偏移量计算索引 printf("标题:%s\n", temp->title); printf("作者:%s\n", temp->author); printf("出版年份:%d\n", temp->year); temp = temp->next; printf("\n"); } } int main() { // 创建3个节点,并将它们连接起来 Node* books[3] = {createNode("书籍1", "作者A", 2020), createNode("书籍2", "作者B", 2018), createNode("书籍3", "作者C", 2016)}; Node* head = books[0]; for (int i = 1; i < 3; i++) { appendToLinkedList(&head, books[i]); } // 输出链表内容 printLinkedList(head); return 0; } ``` 这段代码首先定义了一个链表节点结构,然后创建了三个节点并添加到链表中,最后通过`printLinkedList`函数遍历链表并打印每本书的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值