关于基本实现的一些操作,闲来无事整理了一下,有什么错误欢迎指出
typedef struct Node
{
int date;//data
struct Node *next; //next
}Node, *List;
void Inist_Node(List list)//初始化
{
if (list == NULL)
return;
list->next = NULL;
}
static Node*Create_Node(int val)//创建节点
{
Node*pGet = (Node*)malloc(sizeof(Node));
assert(pGet != NULL);
pGet->date = val;
pGet->next = NULL;
return pGet;
}
bool Insert_Head(List list, int val)//头部插入
{
Node *pcur = Create_Node(val);
pcur->next = list->next;
list->next = pcur;
return true;
}
bool Insert_Tail(List list, int val)//尾部插入
{
Node*p = list;
while (p->next != NULL)
{
p = p->next;
}
Node*pn = Create_Node(val);
p->next = pn;
return true;
}
int Get_NodeLength(List list)//获取链表长度
{
int len = 0;
Node *p = list;
while (p->next != NULL)
{
len++;
p = p->next;
}
return len;
}
bool Insert_pos(List list, int pos, int val)//删除pos位置的元素
{
if (pos > Get_NodeLength(list))
{
return false;
}
Node*p = list;
for (int i = 0; i < pos - 1; i++)//找到pos前驱
{
p = p->next;
}
Node *pcur = Create_Node(val);
pcur->next = p->next;
p->next = pcur;
return true;
}
Node *Search_pre(List list, int key)//查找key元素前驱
{
Node *p = list;
while (p->next != NULL)
{
if (p->next->date == key)
{
return p;
}
p = p->next;
}
return NULL;
}
bool Delete_Node(List list, int val)//从链表中删除key元素
{
Node *p = Search_pre(list, val);
if (p == NULL)
return false;
Node*pdel = p->next;
p->next = pdel->next;
free(pdel);
pdel = NULL;
return true;
}
void Show_Node(List list)
{
Node *p = list->next;
while (p != NULL)
{
cout << p->date << " ";
p = p->next;
}
cout << endl;
}
bool Is_Full(List list)
{
return (list->next == NULL) ? true : false;
}
void Destory_Node(List list)
{
Node *pdel = NULL;
while (list->next != NULL)
{
pdel = list->next;
list->next = pdel->next;
free(pdel);
}
pdel = NULL;
}
接下来包括链表的逆置,删除倒数第K个节点,以及判断是否链表是相交,环,合并有序链表等问题
void Reverse_Node1(List list)//链表逆置
{
Node *p = list->next;
list->next = NULL;
while (p != NULL)
{
Insert_Head(list, p->date);
p = p->next;
}
}
Node *Last_k_Node(List list, int k)//倒数第k个节点前驱
{
if (k<0 || k>Get_NodeLength(list))
{
return NULL;
}
Node *p = list;
Node *q = list;
while (k > 0)
{
if (p->next != NULL)
{
p = p->next;
k--;
}
else
{
return NULL;
}
}
while (p->next != NULL)
{
p = p->next;
q = q->next;
}
return q;
}
void Del_LastK_Value(List list, int k)//删除倒数第k个节点
{
Node *p = Last_k_Node(list, k);
Node *q = p->next;
p->next = q->next;
free(q);
q = NULL;
}
void Create_Cut(List plist, List qlist)//创建相交链表
{
plist->next = qlist->next->next;
}
Node* Is_Cut_abslen(List plist, List qlist)//对齐处理,遍历到最后一个节点判断是否有相交,时间O(len1+len2),优化
{
Node *plong = plist;
Node *pshort = qlist;
int len1 = Get_NodeLength(plist);
int len2 = Get_NodeLength(qlist);
int index_len = len1 - len2;
if (index_len < 0)
{
pshort = plist;
plong = qlist;
index_len = len2 - len1;
}
for (int i = 0; i < index_len; i++)
{
plong = plong->next;
}
while ((plong != pshort) && (plong->next != NULL))
{
plong = plong->next;
pshort = pshort->next;
}
if (plong == pshort && plong != NULL)
{
//return plong;
return pshort;
}
return NULL;
}
bool Is_Cut_LastNode(List plist, List qlist)//遍历最后一个节点
{
if (plist == NULL || qlist == NULL)
{
return false;
}
Node *p1 = plist;
Node *p2 = qlist;
while (p1->next != NULL)
{
p1 = p1->next;
}
while (p2->next != NULL)
{
p2 = p2->next;
}
if (p1 == p2)
{
return true;
}
else
{
return false;
}
}
Node* Is_Cut_with_Stack(List plist, List qlist)//栈处理,从尾部向前pop(),最后一个不同的节点就是第一个相交节点
{
stack<Node*> st1;
stack<Node*> st2;
Node *p = plist;
Node *q = qlist;
while (p->next != NULL)
{
st1.push(p);
p = p->next;
}
while (q->next != NULL)
{
st2.push(q);
q = q->next;
}
Node *index = NULL;
while (st1.top() == st2.top())
{
index = st1.top();
st1.pop();
st2.pop();
}
return index;
}
Node* Baoli_IsCut(List plist, List qlist)//暴力破解,时间复杂度O(n^2)
{
Node *p = plist;
Node *q = qlist;
for (; p->next != NULL; p = p->next)
{
for (; q->next != NULL; q = q->next)
{
if (p == q)
{
return p;
}
}
q = qlist;
}
return NULL;
}
void CreateHoop(List list)//创建环
{
Node *p = list;
while (p->next != NULL)
{
p = p->next;
}
p->next = list->next->next;
}
bool Is_Hoop(List list)//判断是否有环
{
Node *pfast = list;
Node *pslow = list;
while (pfast != NULL && pfast->next != NULL)
{
pfast = pfast->next->next;
pslow = pslow->next;
if (pslow == pfast)
{
return true;
}
}
return false;
}
pair<Node*, int> In_Hoop(List list)//p.first == 环入口节点 p.second == 环长度
{
Node *pfast = list;
Node *pslow = list;
int len = 0;
while (pfast != NULL && pfast->next != NULL)
{
pfast = pfast->next->next;
pslow = pslow->next;
if (pslow == pfast)
{
break;
}
}
pfast = list;
while (pfast != pslow)
{
len++;
pfast = pfast->next;
}
pair<Node*, int> p(pfast, len);
return p;
}
Node *Link_Two_List(List plist, List qlist)//非递归合并两个有序链表
{
Node *p = plist->next;
Node *q = qlist->next;
if (p == NULL && q == NULL)
{
return NULL;
}
if (p == NULL)
{
return q;
}
if (q == NULL)
{
return p;
}
Node *index_Node = ((p->date) < (q->date)) ? plist : qlist;
Node *new_Node = index_Node;
while (p != NULL && q != NULL)
{
if (p->date < q->date)
{
new_Node->next = p;
p = p->next;
}
else
{
new_Node->next = q;
q = q->next;
}
new_Node = new_Node->next;
}
if (p != NULL)
{
new_Node->next = p;
}
if (q != NULL)
{
new_Node->next = q;
}
return index_Node;
}
Node *Link_Two_List2(Node *p, Node *q)//递归连接两个有序链表
{
Node *temp = NULL;
if (p == NULL)
{
return q;
}
if (q == NULL)
{
return p;
}
Node *index = NULL;
if (p->date < q->date)
{
index = p;
index->next = Link_Two_List2(p->next, q);
}
else
{
index = q;
index->next = Link_Two_List2(p, q->next);
}
return index;
}
参考: https://blog.csdn.net/fengxinlinux/article/details/78885764