声明
一年前闲着无聊时,研究研究了链表,当时写的时候也花了不少时间,当时我的思路如下
动态链表带头结点
#include <iostream>
using namespace std;
class node {
public:
int id;
node* next;
};
node* ListCreat() {
node* head = NULL;
head = (node*)malloc(sizeof(node));
if (head == NULL)
{
return NULL;
}
head->id =-1;
head->next = NULL;
node* pCur = head;
node* pNew = NULL;
int data;
while (1) {
cout <<"请输入数据";
cin>>data;
if (data == -1) {
break;
}
pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
continue;
}
pNew->id = data;
pNew->next = NULL;
//链表建立关系
pCur->next = pNew;
pNew->next = NULL;
pCur = pNew;
}
return head;
}
int ListPrint(node* head) {
if (head == NULL)
{
return -1;
}
//取出第一个有效结点,head的下一次结点
node* pCur = head->next;
cout << "head->";
while (pCur != NULL)
{
cout <<pCur->id<<"->";
pCur = pCur->next;
}
cout << "NULL\n";
return 0;
}
int main() {
node* head = NULL;
head = ListCreat();
ListPrint(head);
cout << " ";
system("pause");
return 0;
}
结点的插入
#include <iostream>
using namespace std;
class node {
public:
int id;
node* next;
};
//在值为x的结点前,插入值为y的结点,若x结点不存在,则插在表尾
int increasenode(node* head, int x, int y)
{
if (head == NULL)
{
return -1;
}
node* Pre = head;
node* Cur = head->next;
while (Cur != NULL)
{
if (Cur->id == x)
{
break;
}
Pre = Cur;
Cur = Cur->next;
}
node* pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
return -2;
}
pNew->id = y;
pNew->next = NULL;
Pre->next = pNew;
pNew->next = Cur;
}
node* ListCreat() {
node* head = NULL;
head = (node*)malloc(sizeof(node));
if (head == NULL)
{
return NULL;
}
head->id = -1;
head->next = NULL;
node* pCur = head;
node* pNew = NULL;
int data;
while (1) {
cout << "请输入数据";
cin >> data;
if (data == -1) {
break;
}
pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
continue;
}
pNew->id = data;
pNew->next = NULL;
//链表建立关系
pCur->next = pNew;
pNew->next = NULL;
pCur = pNew;
}
return head;
}
int ListPrint(node* head) {
if (head == NULL)
{
return -1;
}
//取出第一个有效结点,head的下一次结点
node* pCur = head->next;
cout << "head->";
while (pCur != NULL)
{
cout << pCur->id << "->";
pCur = pCur->next;
}
cout << "NULL\n";
return 0;
}
int main() {
node* head = NULL;
head = ListCreat();
ListPrint(head);
int x; int y;
cout << "分别输入x和y,在x的前面插入y" << endl;
cin >> x >> y;
increasenode(head, x, y);
cout << "在x的前面插入y:";
ListPrint(head);
cout << " ";
system("pause");
return 0;
}
静态链表
#include <iostream>
using namespace std;
class node {
public:
int id;
char name[100];
node* next;
};
int main() {
node s1 = { 1,"biaobiao",NULL };
node s2 = { 2,"aha",NULL };
node s3 = { 3,"leilei",NULL };
s1.next = &s2;
s2.next = &s3;
s3.next = NULL;
node* p = &s1;
while (p != NULL) {
cout << p->id << " " << p->name << " " << endl;
p = p->next;
}
}
链表反转
#include<iostream>
using namespace std;
class node {
public:
int id;
node* next;
};
int noderexerse(node* head) {
if (head == NULL || head->next == NULL || head->next->next == NULL)
{
return -1;
}
node* pre = head->next;
node* cur = pre->next;
node* temp=NULL;
while (cur != NULL)
{
temp=cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
head->next->next = NULL;
head->next = pre;
return 0;
}
node* ListCreat() {
node* head = NULL;
head = (node*)malloc(sizeof(node));
if (head == NULL)
{
return NULL;
}
head->id = -1;
head->next = NULL;
node* pCur = head;
node* pNew = NULL;
int data;
while (1) {
cout << "请输入数据";
cin >> data;
if (data == -1) {
break;
}
pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
continue;
}
pNew->id = data;
pNew->next = NULL;
//链表建立关系
pCur->next = pNew;
pNew->next = NULL;
pCur = pNew;
}
return head;
}
int ListPrint(node* head) {
if (head == NULL)
{
return -1;
}
//取出第一个有效结点,head的下一次结点
node* pCur = head->next;
cout << "head->";
while (pCur != NULL)
{
cout << pCur->id << "->";
pCur = pCur->next;
}
cout << "NULL\n";
return 0;
}
int main() {
node* head = NULL;
head = ListCreat();
ListPrint(head);
noderexerse(head);
cout << "反转后为:" << endl;
ListPrint(head);
}
清空动态建立的链表
#include <iostream>
using namespace std;
class node {
public:
int id;
node* next;
};
//清空链表
int destroynode(node* head) {
if (head == NULL) {
return -1;
}
node* temp = NULL;
int i = 0;
while (head != NULL)
{
temp = head->next;
free(head);
head = NULL;
head = temp;
i++;
}
cout << "释放次数为:" << i<<endl;
return 0;
}
node* ListCreat() {
node* head = NULL;
head = (node*)malloc(sizeof(node));
if (head == NULL)
{
return NULL;
}
head->id = -1;
head->next = NULL;
node* pCur = head;
node* pNew = NULL;
int data;
while (1) {
cout << "请输入数据";
cin >> data;
if (data == -1) {
break;
}
pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
continue;
}
pNew->id = data;
pNew->next = NULL;
//链表建立关系
pCur->next = pNew;
pNew->next = NULL;
pCur = pNew;
}
return head;
}
int ListPrint(node* head) {
if (head == NULL)
{
return -1;
}
//取出第一个有效结点,head的下一次结点
node* pCur = head->next;
cout << "head->";
while (pCur != NULL)
{
cout << pCur->id << "->";
pCur = pCur->next;
}
cout << "NULL\n";
return 0;
}
int main() {
node* head = NULL;
head = ListCreat();
ListPrint(head);
destroynode(head);
ListPrint(head);
cout << " ";
system("pause");
return 0;
}
删除第一个为x的结点
#include <iostream>
using namespace std;
class node {
public:
int id;
node* next;
};
int freecode(node*head,int x)
{
if (head == NULL)
{
return -1;
}
node* Pre = head;
node* Cur = head->next;
int flag = 0;//0没有找到,1找到了
while (Cur != NULL)
{
if (Cur->id == x)
{
Pre->next = Cur->next;
free(Cur);
Cur = NULL;
flag = 1;
break;
}
Pre = Cur;
Cur = Cur->next;
}
if (flag == 0)
{
cout << "没有值为" << x << "的结点" << endl;
return -2;
}
}
node* ListCreat() {
node* head = NULL;
head = (node*)malloc(sizeof(node));
if (head == NULL)
{
return NULL;
}
head->id = -1;
head->next = NULL;
node* pCur = head;
node* pNew = NULL;
int data;
while (1) {
cout << "请输入数据";
cin >> data;
if (data == -1) {
break;
}
pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
continue;
}
pNew->id = data;
pNew->next = NULL;
//链表建立关系
pCur->next = pNew;
pNew->next = NULL;
pCur = pNew;
}
return head;
}
int ListPrint(node* head) {
if (head == NULL)
{
return -1;
}
//取出第一个有效结点,head的下一次结点
node* pCur = head->next;
cout << "head->";
while (pCur != NULL)
{
cout << pCur->id << "->";
pCur = pCur->next;
}
cout << "NULL\n";
return 0;
}
int main() {
node* head = NULL;
head = ListCreat();
ListPrint(head);
int x;
cout << "请输入需要删除的结点" << endl;
cin >> x;
freecode(head, x);
cout << "删除后结果为:" << endl;
ListPrint(head);
cout << " ";
system("pause");
return 0;
}
删除所有为x的结点
#include <iostream>
using namespace std;
class node {
public:
int id;
node* next;
};
int freecode(node* head, int x)
{
if (head == NULL)
{
return -1;
}
node* Pre = head;
node* Cur = head->next;
int flag = 0;//0没有找到,1找到了
while (Cur != NULL)
{
if (Cur->id == x)
{
Pre->next = Cur->next;
free(Cur);
Cur = NULL;
flag = 1;
Cur = Pre->next;
continue;
//break;
}
Pre = Cur;
Cur = Cur->next;
}
if (flag == 0)
{
cout << "没有值为" << x << "的结点" << endl;
return -2;
}
}
node* ListCreat() {
node* head = NULL;
head = (node*)malloc(sizeof(node));
if (head == NULL)
{
return NULL;
}
head->id = -1;
head->next = NULL;
node* pCur = head;
node* pNew = NULL;
int data;
while (1) {
cout << "请输入数据";
cin >> data;
if (data == -1) {
break;
}
pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
continue;
}
pNew->id = data;
pNew->next = NULL;
//链表建立关系
pCur->next = pNew;
pNew->next = NULL;
pCur = pNew;
}
return head;
}
int ListPrint(node* head) {
if (head == NULL)
{
return -1;
}
//取出第一个有效结点,head的下一次结点
node* pCur = head->next;
cout << "head->";
while (pCur != NULL)
{
cout << pCur->id << "->";
pCur = pCur->next;
}
cout << "NULL\n";
return 0;
}
int main() {
node* head = NULL;
head = ListCreat();
ListPrint(head);
int x;
cout << "请输入需要删除的结点" << endl;
cin >> x;
freecode(head, x);
cout << "删除后结果为:" << endl;
ListPrint(head);
cout << " ";
system("pause");
return 0;
}
升序链表插入
#include<iostream>
using namespace std;
struct node {
int id;
node* next;
};
//假如原来链表是升序 的,升序后插入新节点
//不能插入结点后再排序,是升序插入新节点x
int insertnode(node* head, int x) {
if (head == NULL)
{
return -1;
}
node* Pre = head;
node* Cur = head->next;
while (Cur != NULL)
{
if (Cur->id > x)
{
break;
}
Pre = Cur;
Cur = Cur->next;
}
node* pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
return -2;
}
pNew->id = x;
pNew->next = NULL;
Pre->next = pNew;
pNew->next = Cur;
}
int nodesort(node* head)
{
if (head == NULL || head->next == NULL)
{
return 0;
}
node* pre = NULL;
node* cur = NULL;
node temp;
for (pre = head->next; pre->next != NULL; pre = pre->next)
{
for (cur = pre->next; cur != NULL; cur = cur->next)
{
if (pre->id > cur->id)
{
temp.id = cur->id;
cur->id = pre->id;
pre->id = temp.id;
/*
temp = *cur;
*cur = *pre;
*pre = temp;
temp.next = cur->next;
cur->next = pre->next;
pre->next = temp.next;*/
}
}
}
return 0;
}
node* ListCreat() {
node* head = NULL;
head = (node*)malloc(sizeof(node));
if (head == NULL)
{
return NULL;
}
head->id = -1;
head->next = NULL;
node* pCur = head;
node* pNew = NULL;
int data;
while (1) {
cout << "请输入数据";
cin >> data;
if (data == -1) {
break;
}
pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
continue;
}
pNew->id = data;
pNew->next = NULL;
//链表建立关系
pCur->next = pNew;
pNew->next = NULL;
pCur = pNew;
}
return head;
}
int ListPrint(node* head) {
if (head == NULL)
{
return -1;
}
//取出第一个有效结点,head的下一次结点
node* pCur = head->next;
cout << "head->";
while (pCur != NULL)
{
cout << pCur->id << "->";
pCur = pCur->next;
}
cout << "NULL\n";
return 0;
}
int main() {
node* head = NULL;
head = ListCreat();
ListPrint(head);
nodesort(head);
int x;
cout << "输入想要升序插入的数:" << endl;
cin >> x;
cout << "升序插入" << x << "后:" << endl;
insertnode(head, x);
ListPrint(head);
}
用链表进行排序
#include<iostream>
using namespace std;
class node {
public:
int id;
node* next;
};
int nodesort(node* head) {
if (head == NULL || head->next == NULL)
{
return 0;
}
node* pre = NULL;
node* cur = NULL;
node temp;
for (pre = head->next; pre->next != NULL; pre = pre->next)
{
for (cur = pre->next; cur != NULL; cur = cur->next)
{
if (pre->id > cur->id)
{
temp.id = cur->id;
cur->id = pre->id;
pre->id = temp.id;
/*
temp = *cur;
*cur = *pre;
*pre = temp;
temp.next = cur->next;
cur->next = pre->next;
pre->next = temp.next;*/
}
}
}
return 0;
}
node* ListCreat() {
node* head = NULL;
head = (node*)malloc(sizeof(node));
if (head == NULL)
{
return NULL;
}
head->id = -1;
head->next = NULL;
node* pCur = head;
node* pNew = NULL;
int data;
while (1) {
cout << "请输入数据";
cin >> data;
if (data == -1) {
break;
}
pNew = (node*)malloc(sizeof(node));
if (pNew == NULL) {
continue;
}
pNew->id = data;
pNew->next = NULL;
//链表建立关系
pCur->next = pNew;
pNew->next = NULL;
pCur = pNew;
}
return head;
}
int ListPrint(node* head) {
if (head == NULL)
{
return -1;
}
//取出第一个有效结点,head的下一次结点
node* pCur = head->next;
cout << "head->";
while (pCur != NULL)
{
cout << pCur->id << "->";
pCur = pCur->next;
}
cout << "NULL\n";
return 0;
}
int main() {
node* head = NULL;
head = ListCreat();
ListPrint(head);
nodesort(head);
cout << "排序后为:";
ListPrint(head);
}
本文介绍了动态链表的创建、节点插入、删除以及链表的反转、清空、升序插入等操作。通过示例代码详细展示了如何实现这些基本的链表操作,包括在特定节点前插入新节点、删除指定节点以及保持链表升序的插入方法。
5万+

被折叠的 条评论
为什么被折叠?



