链表和数组是两个基本的数据结构,两者各有各的优点。
数组:
1.时间复杂度为O(1)的指定位置查找,例如想找到数组中第四个元素,直接数组名[3]就可以找到。
2.时间复杂度为O(n)对数组成员进行增添和删减。
链表:
1.时间复杂度为O(1)对数组成员进行增添和删减。
2.时间复杂度为O(n)的指定位置查找。
链表的概念:
链表其实就像一个绳子连着两个物体一样,然后一直连下去。
链表的创建:
链表的结构类型就是由结构体变量组成的,链表就是由这么很多个结构体变量连接而成的,我们又乘这样的结构体变量为结点,每个结点当然要储存一个数,所以要创建一个数据结构,因为有着链式的效果,我们需要知道下一个结点的地址,所以创建一个指针来存储下一个结点的地址,用typedef更方便我们书写,如下:
typedef struct Node
{
int date;
struct Node* next;
}Node;
首先得创建一个头结点,起码知道起始点才能进行下面的操作嘛。
Node* headNode = (Node*)malloc(sizeof(Node));
然后创建链表,date为链表的长度:
void createList(Node* headNode, int date)
{
Node* rear = headNode;
for (int i = 0; i < date; i++)
{
Node* NewNode = (Node*)malloc(sizeof(Node));//创建一个新结点
NewNode->next = NULL;
scanf_s("%d", &NewNode->date);
rear->next = NewNode;
rear = NewNode;
}
}
链表的遍历:
创建完后,遍历就很容易了:
void traveList(Node* headNode)
{
Node* Move = headNode->next;
while (Move)
{
printf("%d ", Move->date);
Move = Move->next;
}
cout << endl;
}
接下来放在一个完整的代码里: ?
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct Node
{
int date;
struct Node* next;
}Node;
void createList(Node* headNode, int date)
{
Node* rear = headNode;
for (int i = 0; i < date; i++)
{
Node* NewNode = (Node*)malloc(sizeof(Node));
NewNode->next = NULL;
scanf_s("%d", &NewNode->date);
rear->next = NewNode;
rear = NewNode;
}
}
void traveList(Node* headNode)
{
Node* Move = headNode->next;
while (Move)
{
printf("%d ", Move->date);
Move = Move->next;
}
cout << endl;
}
void insertForward(Node* headNode, int date)
{
Node* NewNode = (Node*)malloc(sizeof(Node));
NewNode->next = NULL;
NewNode->date = date;
NewNode->next = headNode->next;
headNode->next = NewNode;
}
void insertBack(Node* headNode, int date)
{
Node* NewNode = (Node*)malloc(sizeof(Node));
NewNode->date = date;
NewNode->next = NULL;
Node* Move = headNode;
while (Move->next)
{
Move = Move->next;
}
Move->next = NewNode;
}
void insertzj(Node* headNode, int weizhi, int date)
{
int a = 0;
Node* NewNode = (Node*)malloc(sizeof(Node));
NewNode->next = NULL;
NewNode->date = date;
Node* Move1 = headNode;
Node* Move2 = headNode->next;
while (a != weizhi)
{
Move1 = Move1->next;
Move2 = Move2->next;
a++;
}
NewNode->next = Move2;
Move1->next = NewNode;
}
void reverseList(Node* headNode)
{
Node* hou = NULL;
Node* qian = NULL;
while (headNode->next)
{
hou = headNode->next;
headNode->next = hou->next;
hou->next = qian;
qian = hou;
}
headNode->next = qian;
}
void deleteSame(Node* head) {
Node* curr = head->next;
while (curr) {
Node* pre = curr;
Node* p = curr->next;
while (p) {
if (curr->date == p->date)
{
pre->next = p->next;
free(p);
p = pre->next;
}
else
{
pre = pre->next;
p = p->next;
}
}
curr = curr->next;
}
}
int main()
{
Node* headNode = (Node*)malloc(sizeof(Node));
headNode->next = NULL;
createList(headNode, 10);//链表的创建
traveList(headNode);//链表的遍历
insertForward(headNode, 100);//头插法
traveList(headNode);
insertBack(headNode, 200);//尾插法
traveList(headNode);
insertzj(headNode, 2, 300);//从中间插入法
traveList(headNode);
reverseList(headNode);//翻转链表
traveList(headNode);
deleteSame(headNode);//删除链表中相同的元素
traveList(headNode);
return 0;
}
|