栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++单链表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++单链表

完善上一篇的单链表

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
using namespace std;
struct node
{
	int data;
	struct node* next;
};
struct node* head;
struct node* Createlink(struct node* head,int n) {
	head = (struct node*)malloc(sizeof(struct node));
	head->next = NULL;
	struct node* tail = head;
	while (n-->0)
	{
		struct node* p= (struct node*)malloc(sizeof(struct node));
		cin >> p->data;
		p->next = NULL;
		tail->next = p;
		tail = p;
	}
	return head;
}
void Print(struct node* head) {
	struct node* p = head;
	p = p->next;
	while (p!=NULL)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
void h_insert(struct node* head) {
	struct node* p = (struct node*)malloc(sizeof(struct node));
	cin >> p->data;
	struct node* temp = head->next;
	head->next = p;
	p->next = temp;
}
void t_insert(struct node* head) {
	struct node* p = head;
	while (p->next!=NULL)
	{
		p = p->next;
	}
	struct node* temp = (struct node*)malloc(sizeof(struct node));
	cin >> temp->data;
	temp->next = NULL;
	p->next = temp;
}
void h_delete(struct node* head) {
	struct node* temp = head->next;
	head->next = temp->next;
	free(temp);
}
void t_delete(struct node* head) {
	struct node* p = head;
	struct node* temp = head;
	while (p->next != NULL)
	{
		temp = p;
		p = p->next;
	}
	temp->next = NULL;
	free(p);
}
void index_delete(struct node* head,int x) {
	struct node *p, *q;
	p = head;
	while (p->next!=NULL)
	{
		q = p->next;
		if (q->data == x) {
			p->next = q->next;
			free(q);
			q->next = NULL;
			continue;
		}
		p = p->next;
	}
}
void index_insert(struct node* head,int index)
{
	int i = 0;
	struct node* p = head;
	while (p != NULL && i < index)
	{
		p = p->next;
		i++;
	}
	if (p==NULL||i>index)
	{
		cout<<"数组越界"<> q->data;
	struct node* temp = p->next;
	p->next = q;
	q->next = temp;
}
int main() {
	int n;
	cin >> n;
	head=Createlink(head, n);
	index_insert(head,2);
	Print(head);
	system("pause");
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号