链表的用法

久违的链表  mua mua mua !!!
#include<bits/stdc++.h>
using namespace std;

struct node{
	int data;    // 单链表的数据域 
	struct node *Next;  // 单链表的指针域 
};
 
/**
建立1:头插,逆序 
*/

node *creat1(int n){
	node *head;
	head=(node*)malloc(sizeof(node));
	head->Next=NULL;

	for(int i=1;i<=n;i++){
		node *p;
		p=(node *)malloc(sizeof(node));
		int x;
		cin>>x;
		p->data=x;
		p->Next=head->Next;
		head->Next=p;
	}
	return head;
}

/**
建立2:尾插 ,顺序 
*/
node *creat2(int n){
	node *head;
	head=(node *)malloc(sizeof(node));
	head->Next=NULL;
	node *q;
	q=head;
	for(int i=1;i<=n;i++){
		node *p;
		p=(node *)malloc(sizeof(node));
		int x;
		cin>>x;
		p->data=x;
		q->Next=p;
		q=p;
	}
	q->Next=NULL;
	return head;
}
/**
插入  位置:pos  插入val 
*/ 


node *add(node *head,int pos,int val,int n){
	node *pre;
	pre=head;
	for(int i=1;i<pos;i++){
		pre=pre->Next;
	}
	node *p;
	p=(node *)malloc(sizeof(node));
	p->data=val;
	p->Next=pre->Next;
	pre->Next=p;
	return head;
}


/**
删除 值为val的元素 
*/
node *Delete_val(node *head,int val){
	node *p,*pre;
	p=head->Next;
	pre=head;
	while(p->data!=val){
		pre=p;
		p=p->Next;
	} 
	pre->Next=p->Next;
	free(p);
	return head;
}

void print(node *p){
	p=p->Next;
	if(p) cout<<p->data;
	p=p->Next;
	while(p){
		cout<<" "<<p->data;
		p=p->Next;
	}
	cout<<endl;
}

/**
删除  位置 pos  
*/

node *Delete_pos(node *head,int pos){
	node *p,*pre;
	p=head->Next;
	pre=head;
	int i=1;
	while(i<pos){
		i++;
		pre=p;
		p=p->Next;
	}
	pre->Next=p->Next;
	free(p);
	return head;
}

int main (){
	int n;
	cin>>n;
	/**
	node *head ;
	head=creat1(n);
	print(head);
	*/
	node *head;
	head =creat2(n);
	//print(head);
	int m;
	cin>>m;
	for(int i=0;i<m;i++){
		/*int x,val;
		cin>>x>>val;
		x++;
		head=add(head,x,val,n);*/
		char ch[3];
		scanf("%s",ch);
		if(ch[0]=='D'){
			int st,ed;
		    cin>>st>>ed;
		    int k=ed-st+1;
		    while(k--){
			    head=Delete_pos(head,st);
		    }
		}
		else if(ch[0]=='I'){
			int st,sum;
			cin>>st>>sum;
			while(sum--){
				int val;
				cin>>val;
				head=add(head,++st,val,n);
			}
		}
		/**int st,ed;
		cin>>st>>ed;
		int k=ed-st+1;
		while(k--){
			head=Delete_pos(head,st);
		}*/
		
		/*char ch[3];
		scanf("%s",ch);
		if(ch[0]=='D'){
			int pos;
			cin>>pos;
			head=Delete_pos(head,pos);
		} 
		else if(ch[0]=='I'){
			int a,b;
			cin>>a>>b;
			head=add(head,a+1,b,n);
		}*/
		/**
		int pos;
		cin>>pos;
		head=Delete_pos(head,pos);*/
	}
	print(head);
	return 0;
} 




/**
双向链表建立:尾插 顺序 
*/
node *creat2(int n){
	node *head;
	head=(node *)malloc(sizeof(node));
	head->Next=NULL;
	head->pre=NULL;
	node *q;
	q=head;
	for(int i=1;i<=n;i++){
		node *p;
		p=(node *)malloc(sizeof(node));
		int x;
		cin>>x;
		p->data=x;
		p->Next=NULL;
		p->pre=q;
		q->Next=p;
		q=p;
	}
	q->Next=NULL;
	return head;
}
#include<bits/stdc++.h>
using namespace std;

struct node{
	int data;    // 单链表的数据域 
	struct node *Next;  // 单链表的指针域 
};
 

/**
循环链表建立:尾插 顺序 
*/
node *creat2(int n){
	node *head;
	head=(node *)malloc(sizeof(node));
	head->data=1; 
	head->Next=NULL;
	node *q;
	q=head;
	for(int i=2;i<=n;i++){
		node *p;
		p=(node *)malloc(sizeof(node));
		p->data=i;
		p->Next=NULL;
		q->Next=p;
		q=p;
	}
	q->Next=head;
	return head;
}

int Count(node *head ,int n){
	node *p,*q;
	q=head;
	while(q->Next!=head) q=q->Next;
	for(int i=1;;i++){
		p=q->Next;
		if(i%5==0){
			if(p->data==1) return i/5;
			q->Next=p->Next;
		}
		else q=p;
	}
}

int main (){
	int n;
	while(cin>>n){
		if(n==0) break;
	    node *head;
	    head =creat2(n);
	    cout<<Count(head,n)<<endl;
	} 
	
	
	return 0;
} 


<think>好的,我需要仔细回答用户关于C语言链表使用方法的问题。用户可能刚开始学习数据结构,对链表的概念和操作还不熟悉,所以应该从基础开始讲起,逐步深入。 首先,我应该解释链表的基本概念,比如节点结构,每个节点包含数据和指针。然后,对比数组,说明链表的优势,比如动态大小和插入删除的高效性。接着,详细说明如何创建链表,包括结构体定义、节点创建和链接。这部分可能需要代码示例,但用户要求使用中文,所以需要结合代码片段来解释。 接下来是常见的链表操作,比如遍历、插入、删除和释放内存。每个操作都需要分步骤说明,并给出示例代码。需要注意内存管理的部分,强调malloc和free的使用,避免内存泄漏。此外,用户可能需要了解头节点的重要性,以及如何处理空链表的情况。 用户可能对指针操作不太熟悉,尤其是双向链表,所以这部分需要更详细的解释。可能遇到的问题,如头指针的处理、边界条件(头节点和尾节点的操作)也需要提及。最后,总结链表的应用场景,帮助用户理解何时使用链表更合适。 需要确保回答结构清晰,步骤分明,语言简洁,避免过于技术化的术语,同时保持准确性。检查是否有遗漏的重要知识点,比如循环链表或双向链表的简要介绍,但可能用户主要关注单链表的基本使用,所以先集中在单链表上,再简要提及其他类型作为扩展。 最后,提醒用户注意常见错误,如野指针、内存泄漏,并给出学习建议,比如调试方法和练习题目。这样用户能够全面理解链表使用方法,并能够自己动手实践。</think>C语言链表使用方法详解(分步骤说明): 一、链表基本概念 1. 链表组成:由多个节点(Node)通过指针连接而成 2. 节点结构: $$struct Node \{$$ $$ int data; // 数据域$$ $$ struct Node* next; // 指针域$$ $$\};$$ 3. 与数组对比优势: - 动态内存分配(无需预先知道数据量) - 插入/删除效率高(时间复杂度O(1)) 二、创建链表步骤 1. 定义节点结构体 ```c struct Node { int data; struct Node* next; }; ``` 2. 创建头节点 ```c struct Node* head = NULL; // 初始化空链表 ``` 3. 创建新节点(示例) ```c struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = 10; newNode->next = NULL; ``` 三、常见操作 1. 插入节点(头插法) ```c void insertAtHead(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = *head_ref; *head_ref = new_node; } ``` 2. 遍历链表 ```c void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } ``` 3. 删除节点 ```c void deleteNode(struct Node** head_ref, int key) { struct Node* temp = *head_ref, *prev; // 头节点即为目标节点 if(temp != NULL && temp->data == key) { *head_ref = temp->next; free(temp); return; } // 查找要删除的节点 while(temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if(temp == NULL) return; prev->next = temp->next; free(temp); } ``` 四、内存管理要点 1. 创建节点必须使用`malloc`动态分配内存 2. 删除节点后必须使用`free`释放内存 3. 操作结束后应遍历释放整个链表 ```c void freeList(struct Node* head) { struct Node* tmp; while(head != NULL) { tmp = head; head = head->next; free(tmp); } } ``` 五、使用注意事项 1. 指针操作要避免野指针 2. 注意处理空链表的情况(head == NULL) 3. 插入/删除时注意维护链表连续性 4. 建议先画示意图再写代码 六、扩展类型 1. 双向链表:增加前驱指针 $$struct Node \{$$ $$ int data;$$ $$ struct Node* prev;$$ $$ struct Node* next;$$ $$\};$$ 2. 循环链表:尾节点指向头节点 七、典型应用场景 1. 实现栈/队列等数据结构 2. 动态增减数据集合 3. 内存管理系统中的空闲内存管理 4. 文件系统的目录结构 常见错误提醒: - 忘记更新指针导致链表断裂 - 内存泄漏(未正确释放节点) - 访问已释放的内存空间 - 未正确处理头节点指针 学习建议: 1. 使用调试工具逐步跟踪指针变化 2. 从简单操作开始练习(如创建3节点链表) 3. 尝试实现反转链表、检测环等经典算法题 通过掌握这些基础操作和注意事项,您已经能够使用C语言实现基本链表功能。实际开发中建议结合具体需求选择合适的链表类型,并做好错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值