C++ 单链表创建、插入和删除

 该代码转载而来,找不到出处了,可能含较多BUG与不住,仅提供一种思路。

#include <iostream>
#include <stdio.h>
#include <string>
#include <conio.h>

/**
* cstdio是将stdio.h的内容用C++头文件的形式表示出来。
*stdio.h是C标准函数库中的头文件,即:standard buffered input&output。
*提供基本的文字的输入输出流操作(包括屏幕和文件等)。
*/

/**
*conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,
*主要是一些用户通过按键盘产生的对应操作,比如getch()()函数等等。
*/

using namespace std;

struct node
{
	int data;
	node *next;
};
typedef struct node node, *list;

// 创建单链表
node *creat()
{
	node *head, *p;
	head = new node;
	p = head;

	int x, cycle = 1;
	while (cycle)
	{
		cout << "Please input the data for single linker : ";
		cin >> x;

		if (x != 0)
		{
			node *s = new node;
			s->data = x;
			cout << "Input data : " << x << endl;

			p->next = s;
			p = s;
		}
		else
		{
			cycle = 0;
			cout << "Input done! " << endl;
		}
	}

	head = head->next;
	p->next = NULL;
	//cout << "\nFirst data of single linker is " << head->data << endl;

	return head;
}

// 单链表测长
int length(node *head)
{
	int n = 0;
	node *p = head;

	while (p != NULL)
	{
		p = p->next;
		n++;
	}

	return n;
}

// 单链表打印
void printL(node *head)
{
	node *p = head;

	while (p != NULL)
	{
		cout << "Single Linker data is " << p->data << endl;
		p = p->next;
	}
}

// 单链表插入
node *insert(node *head, int num)
{
	node *p0, *p1, *p2;
	p1 = head;

	p2 = new node;
	p0 = new node; // 插入节点
	p0->data = num;// 插入数据

	while (p0->data > p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;// p0,p1和p2位置: p2->p1->p0
	}

	if (p0->data <= p1->data)
	{
		if (p1 == head)
		{// 头部前段插入 p0和p1位置: p0->p1->...
			head = p0;
			p0->next = p1;
		}
		else
		{// 插入中间节点 p0,p1和p2位置: p2-> p0 -> p1
			p2->next = p0;
			p0->next = p1;
		}
	}
	else
	{   // 尾部插入节点 p0,p1和p2位置: p2->p1->p0->NULL
		p1->next = p0;
		p0->next = NULL;
	}
	return head;
}

// 单链表删除
node *del(node *head, int num)
{
	node *p1, *p2;
	p2 = new node;
	p1 = head;

	while (num != p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;// p1和p2位置: p2->p1		
	}

	if (num == p1->data)
	{
		if (p1 == head)// 删除头节点
		{
			head = p1->next;
			delete p1;
		}
		else
		{
			p2->next = p1->next;
			delete p1;
		}
	}
	else
	{
		cout << num << " could not been found in the current single linker!" << endl;
	}
	return head;
}

//=============插入排序====================
node *insertSort( node *head )
{
	node  *p1, *prep1, *p2, *prep2, *temp;
	prep1 = head->next;
	p1 = prep1->next;
	//prep1和p1是否需要手动后移
	bool flag;

	while (p1 != NULL)
	{
		flag = true;
		temp = p1;
		//由于是单向链表,所以只能从头部开始检索
		for (prep2 = head, p2 = head->next; p2 != p1; prep2 = prep2->next, p2 = p2->next)
		{
			//发现第一个较大值
			if (p2->data > p1->data)
			{
				p1 = p1->next;
				prep1->next = p1;
				prep2->next = temp;
				temp->next = p2;
				flag = false;
				break;
			}
		}
		//手动后移prep1和p1
		if (flag)
		{
			prep1 = prep1->next;
			p1 = p1->next;
		}
	}
	return head;
}

int main()
{
	cout << "***创建单链表***" << endl;
	node *head = creat();
	cout << endl;

	cout << "***计算链表长***" << endl;
	int n = length(head);
	cout << "The length of input single linker is " << n << "." << endl;
	cout << endl;

	cout << "***打印单链表***" << endl;
	printL(head);
	cout << endl;

	cout << "****插入节点****" << endl;
	cout << "Please input the data for inserting operate : ";
	int inData;
	cin >> inData;
	head = insert(head, inData);
	printL(head);
	cout << endl;

	cout << "****删除节点****" << endl;
	cout << "Please input the data for deleting operate : ";
	int outData;
	cin >> outData;
	head = del(head, outData);
	printL(head);
	cout << endl;

	cout << "****进行排序****" << endl;
	//第一位地址可以存放指示器,从第二位开始保存数据
	node *mylist = new node[sizeof(node)];
	mylist->data = 0;
	mylist->next = NULL;

	int len = length(head);
	int i = 0;
	node * cur = mylist;


	node *headcopy = head;
	while (len--)
	{
		//node * newNode = (node *)malloc(sizeof(node));   
		node *newNode = new node[sizeof(node)];            
		newNode->data = headcopy->data;
		newNode->next = NULL;
		cur->next = newNode;
		cur = cur->next;
		headcopy=headcopy->next;
	}
	head = insertSort(mylist);
	head = del(head, 0);
	printL(head);

	return 0;
}

1.头节点插入和删除结果

 

2.中间节点插入和删除结果

 

3.尾结点插入和删除结果

  • 29
    点赞
  • 130
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
以下是C++实现单链表插入删除操作的示例代码: ```c++ #include<iostream> using namespace std; //定义链表结构体 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; //链表插入操作 void insertNode(ListNode* &head, int val) { ListNode* newNode = new ListNode(val); if(head == NULL) { head = newNode; } else { ListNode* cur = head; while(cur->next != NULL) { cur = cur->next; } cur->next = newNode; } } //链表删除操作 void deleteNode(ListNode* &head, int val) { if(head == NULL) return; if(head->val == val) { head = head->next; } else { ListNode* cur = head; while(cur->next != NULL && cur->next->val != val) { cur = cur->next; } if(cur->next != NULL) { cur->next = cur->next->next; } } } //链表遍历 void printList(ListNode* head) { ListNode* cur = head; while(cur != NULL) { cout << cur->val << " "; cur = cur->next; } } int main() { ListNode* head = NULL; insertNode(head, 1); insertNode(head, 2); insertNode(head, 3); insertNode(head, 4); cout << "插入操作后的链表为:"; printList(head); deleteNode(head, 2); deleteNode(head, 4); cout << "\n删除操作后的链表为:"; printList(head); return 0; } ``` 在上述代码中,我们首先定义了一个链表结构体 `ListNode`,其中包含节点的值 `val` 和指向下一个节点的指针 `next`。然后,我们实现了链表插入操作 `insertNode` 和删除操作 `deleteNode`。最后,我们通过 `printList` 函数遍历链表,输出链表中的每个节点的值。 在 `main` 函数中,我们先创建一个空链表 `head`,然后依次插入节点,最后输出插入节点后的链表。然后,我们删除链表中的节点2和节点4,并输出删除节点后的链表。 运行上述代码,输出结果如下: ``` 插入操作后的链表为:1 2 3 4 删除操作后的链表为:1 3 ``` 可以看到,我们成功地实现了单链表插入删除操作,并正确输出了链表中节点的值。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值