链表基本操作
1.链表结构体定义
struct Node{
int data;
Node*next;
}
2.不需要指针的基本操作
bool EmptyList(Node* head) {
if (head->next == NULL) {
return true;
}
return false;
}
int LinkLength(Node* head) {
return head->data;
}
3.需要一个指针的基本操作
void GetElem(Node* head, int i,int& a) {
Node* p;
int j = 1;
p = head->next;
while (p && j < i) {
p = p->next;
j++;
}
if (!p || j > i)
return;
a = p->data;
return;
}
int LocateElem(Node* head, int a) {
Node* p;
int j = 1;
p = head->next;
while (p && p->data != a) {
p = p->next;
j++;
}
if (!p)
return 0;
return j;
}
4.需要两个指针的基本操作
void CreatLink(Node*& head, int n) {
head = new Node;
head->next = NULL;
head->data = n;
Node* pre, * p;
pre = head;
for (int i = 0; i < n; i++) {
p = new Node;
cin >> p->data;
p->next = pre->next;
pre->next = p;
pre = p;
}
}
void ClearLink(Node*& head) {
Node* pre, * p;
pre = head->next;
while (pre) {
p = pre->next;
delete pre;
pre = p;
}
head->next = NULL;
}
int PriorElem(Node* head, int a) {
Node* pre, * p;
pre = head;
p = head->next;
while (p && p->data != a) {
pre = p;
p = pre->next;
}
return pre->data;
}
int NextElem(Node* head, int a) {
Node* pre, * p;
pre = head->next;
p = pre->next;
while (p && a != pre->data) {
pre = p;
p = pre->next;
}
return p->data;
void InsertElem(Node*& head, int i, int a) {
Node* pre, * p;
pre = head->next;
int j = 1;
while (j < i - 1) {
pre = pre->next;
j++;
}
p = new Node;
p->data = a;
p->next = pre->next;
pre->next = p;
head->data++;
}
void DeleteElem(Node* head, int i, int& a) {
Node* pre, * p;
int j = 1;
pre = head;
p = pre->next;
while (j<i) {
pre = p;
p = pre->next;
j++;
}
pre->next = p->next;
a = p->data;
delete p;
}
void ResortLink(Node*& head) {
Node* pre, * p;
pre = head->next;
head->next = NULL;
while (pre) {
p = pre->next;
pre->next = head->next;
head->next = pre;
pre = p;
}
}
5.需要三个指针的基本操作
void DeleteSame(Node* head) {
Node* elem, * pre, * p;
elem = head->next;
while (elem) {
pre = elem;
p = pre->next;
while (p && p->data != elem->data) {
pre = p;
p = pre->next;
}
if (p) {
pre->next = p->next;
delete p;
head->data--;
}
elem = elem->next;
}
}
6.需要四个指针的基本操作
void SelectSort(Node*& head) {
Node* max, *maxpre, *pre, *p;
Node* head1 = new Node;
head1->next = NULL;
maxpre = head;
max = maxpre->next;
while (max) {
pre = max;
p = pre->next;
while (p) {
if (p->data > max->data) {
maxpre = pre;
max = maxpre->next;
}
pre = p;
p = pre->next;
}
maxpre->next = max->next;
max->next = head1->next;
head1->next = max;
maxpre = head;
max = maxpre->next;
}
head->next = head1->next;
delete head1;
}
void InsertSort(Node*& head) {
Node* flag, * preflag, * pre, *p;
pre = head->next->next;
p = pre->next;
head->next->next = NULL;
preflag = head;
flag = preflag->next;
while (1) {
while (flag) {
if (pre->data < flag->data) {
pre->next = preflag->next;
preflag->next = pre;
break;
}
preflag = flag;
flag = preflag->next;
}
if (!flag) {
pre->next = preflag->next;
preflag->next = pre;
}
preflag = head;
flag = preflag->next;
pre = p;
if (pre == NULL)
break;
p = pre->next;
}
}
void InsertSort(Node*& head1,Node* head2) {
Node* flag, * preflag, * pre, *p;
pre = head2;
p = pre->next;
preflag = head1;
flag = preflag->next;
while (1) {
while (flag) {
if (pre->data < flag->data) {
pre->next = preflag->next;
preflag->next = pre;
break;
}
if(pre->data== flag->data)
break;
preflag = flag;
flag = preflag->next;
}
if (!flag) {
pre->next = preflag->next;
preflag->next = pre;
}
preflag = head;
flag = preflag->next;
pre = p;
if (pre == NULL)
break;
p = pre->next;
}
}