链表的详细总结
#include"pch.h"
#include <bits/stdc++.h>
typedef int elementType;
typedef struct Node {
elementType value;
struct Node *next;
}nodelist;
nodelist *newnode(int value) {
nodelist *node;
if (!(node = (nodelist*)malloc(sizeof(nodelist))))
return NULL;
node->value = value;
node->next = NULL;
return node;
}
nodelist *headInsert(nodelist*head, int value) {
nodelist*node=NULL;
if(!(newnode(value)))
return NULL;
if(head)
node->next = head;
head = node;
return head;
}
nodelist*tailListInsert(nodelist*head, nodelist*node) {
nodelist*current = head;
if (!head)
head=node;
else {
while (current->next)
current = current->next;
node->next = NULL;
current->next = node;
}
return head;
}
nodelist*tailValueInsert(nodelist*head, int value) {
nodelist*node;
if (!(node = newnode(value)))
return NULL;
return tailListInsert(head, node);
}
nodelist*deleteNode(nodelist*head, int value) {
nodelist*current = head;
while (current->next) {
current = current->next;
if (current->next->value == value) {
nodelist*temp = current->next;
current->next = current->next->next;
free(temp);
break;
}
}
return head;
}
nodelist*printNode(nodelist*head) {
nodelist*current = head;
if (!head)
return NULL;
else {
while(current){
printf_s("%-2d->", current->value);
current = current->next;
if (current->next == head)
break;
}
}
return head;
}
nodelist*listCreatArray(int a[]) {
int len = sizeof(a) / sizeof(a[0]);
nodelist*head=NULL;
for (int i = 0; i < len; i++) {
if (!(head = tailValueInsert(head, a[i])))
return NULL;
}
return head;
}
int lengthNode(nodelist*head) {
int sum = 0;
if (!head)
return 0;
else {
nodelist*current = head;
while (current) {
++sum;
current = current->next;
}
return sum;
}
}
nodelist*reverseNode(nodelist*head) {
nodelist*current=head, *newHead=NULL;
while (current) {
nodelist*next = current->next;
current->next = newHead;
newHead = current;
current = next;
}
return newHead;
}
nodelist*mergeNode(nodelist*a, nodelist*b) {
nodelist *c=NULL;
nodelist*tail = c;
if (!a)
return b;
if (!b)
return a;
while (a&&b) {
if (a->value > b->value) {
tail->next = b;
tail = b;
b = b->next;
}
if (a->value < b->value) {
tail->next = a;
tail = a;
a = a->next;
}
}
if (a)
tail->next = a;
if (b)
tail->next = b;
return c;
}
nodelist*copyNode(nodelist*head) {
nodelist*current = head, *newhead = NULL;
if (head == NULL || head->next == NULL)
return head;
else {
while (current) {
nodelist*next = current->next;
current->next = newhead;
newhead = current;
current = next;
}
return newhead;
}
}
bool IntersertNode(nodelist*a, nodelist*b) {
int len = abs(lengthNode(a)-lengthNode(b));
nodelist*longnode = a, *shortnode = b;
if (lengthNode(b) > lengthNode(a))
longnode = b, shortnode = a;
for (int i = 0; i < len; i++) {
longnode = longnode->next;
}
while (longnode&&shortnode) {
if (longnode == shortnode) {
return true;
break;
}
longnode = longnode->next;
shortnode = shortnode->next;
}
return false;
}
nodelist* ringNode(nodelist*head) {
nodelist*p, *q;
p = q = head;
while (p&&q&&q->next) {
p = p->next;
q = q->next->next;
if (p == q)
return q;
}
return NULL;
}
nodelist*startNode(nodelist*head) {
nodelist*meeting ,*current=head;
if (!(meeting = ringNode(head)))
return NULL;
while (meeting&¤t) {
if (meeting == current)
break;
current = current->next;
meeting = meeting->next;
}
return current;
}
nodelist *listEnumarateAdd(nodelist *list1, nodelist *list2)
{
int carry = 0;
nodelist *result = NULL;
while (list1 || list2 || carry) {
int value = carry;
if (list1) {
value += list1->value;
list1 = list1->next;
}
if (list2) {
value += list2->value;
list2 = list2->next;
}
result = tailValueInsert(result, value % 10);
carry = (value >= 10 ? 1 : 0);
}
return result;
}
nodelist*insertNodeNoRing(nodelist*head, int value) {
nodelist*current = head;
nodelist*node = newnode(value);
if (!head || head->value>value) {
node->next = head;
head = node;
}
else {
while (current->next!=NULL&¤t->next->value<value)
current = current->next;
node->next = current->next->next;
current->next = node;
}
return head;
}
nodelist*insertNodeRing(nodelist*head, int value) {
nodelist*node = newnode(value),*pre=NULL,*current=head;
do {
pre = current;
current = current->next;
if (value<current->value&&value>pre->value)
break;
} while (current != head);
if (value < head->value || current==head)
head = node;
return head;
}
nodelist*findReNode(nodelist*head, int K) {
nodelist*p, *q;
p = q = head;
for (int i = 0; i < K; i++)
p=p->next;
while (p&&q) {
p = p->next;
q = q->next;
}
return q;
}
nodelist*initList(int n) {
nodelist*head = (nodelist*)malloc(sizeof(nodelist));
head->value = 1;
head->next = NULL;
nodelist*current = head;
for (int i = 2; i < n ; i++) {
nodelist*node= (nodelist*)malloc(sizeof(nodelist));
node->value = i;
node->next = NULL;
current->next = node;
current = current->next;
}
current->next = head;
return head;
}
int removeNode(nodelist*head, int m, int k) {
nodelist*current = head,*temp=NULL;
for (int i = 0; i < m; i++)
current = current->next;
nodelist*node = current;
while (node->next != node) {
for (int i = 0; i < k; i++) {
temp = node;
node = node->next;
}
temp->next = node->next;
printf_s("%d -> ", node->value);
free(node);
node = temp->next;
}
return node->value;
}
int main()
{
nodelist*head=initList(35);
int n=removeNode(head, 7, 5);
std::cout << n << std::endl;
system("pause");
return 0;
}