链表的基本操作
存档,分析日后补上
创建空链表
NODE* Initlist() {
NODE* head;
head = new NODE;
head->data = 0;
head->next = NULL;
return head;
}
创建无序链表
NODE* Creat(NODE *head) {
NODE* p1,*p2=head;
int a;
int num = 0;
cout << "Input a data (stop when a=-1):";
cin >> a;
while (a != -1) {
num++;
p1 = new NODE;
p1->data = a;
p2->next = p1;//不要忘记要把新开辟的结点和已有的最后一个结点相连
p2 = p1;
cout << "Input a data (stop when a=-1):";
cin >> a;
}
p2->next = NULL;
head->data = num;//head中的数据存储了链表中的数据数
cout << "\nnum=" << num << '\n';
return head;
}
链表的排序
这里采用了排序的一种方法,先把链表内容输入一维数组(动态分配内存),再调用一个自定义的通用排序函数对数组进行排序,最后按顺序将数组内的数重新输入链表中。
NODE* Sort(NODE* head) {
NODE* p;
int i = 0;
p = head;
if (head->data == 0) {
cout << "The list is empty!"; return head;
}
int *Sort = new int[head->data];
for (i = 0; i < head->data; i++) {
p = p->next;
Sort[i] = p->data;
}
p = head;
SortI(Sort, head->data);
for (i = 0; i < head->data; i++) {
p = p->next;
p->data = Sort[i];
}
return head;
}
这里我写的排序方式是插入排序,代码如下:
void SortI(int *a, int n) {
int i, j,k = 0;
int x;
for (i = 0; i < n - 1; i++) {
x = a[i+1];
for (j = 0; j <= i && x >= a[j]; j++);
for (k = i; k >=j; k--) {
a[k + 1] = a[k];
}
a[j] = x;
}
}
在排好序的链表中插入一个新结点
NODE* Insert(NODE* head, NODE* s) {//升序排列
NODE* p;
p = head;
while (p->next != NULL && p->next->data < s->data) {
p = p->next;
}
s->next = p->next;
p->next = s;
return head;
}
搜索一个数字是否出现在链表中
NODE* Search(NODE* head, int x) {
NODE* p;
p = head->next;
while (p != NULL) {
if (p->data == x) { return p; }
else p = p->next;
}
return NULL;
}
删除链表中的一个数
NODE* Delete(NODE* head, int x) {
NODE* p,*s;
p = head;
while(p->next!=NULL&&p->next->data!=x){
p = p->next;
}
if (p->next == NULL) { cout << "Delete unsuccessfully!"; return head; }
else {
s = p->next;
p->next = s->next;
delete s;
cout << "Delete successfully!\n";
}
return head;
}
链表打印
void Print(NODE* head) {
NODE* p;
p = head->next;
if (p != NULL) {
cout << "Output list:\n";
while (p != NULL) {
cout <<setw(5)<< p->data;
p = p->next;
}
cout << '\n';
}
}
链表的释放
释放后的链表再打印会报错。
NODE* Free(NODE* head) {
NODE* p;
while (head) {
p = head;
head = head->next;
delete p;
}
return head;
}
主函数部分
#include <iostream>
#include <iomanip>
using namespace std;
#define LEN sizeof(NODE)
typedef struct node//定义节点,此后链表的结点类型可以写为NODE
{
int data;
node* next;
}NODE;
NODE* Initlist();//创建空列表。这是一个返回指针值的函数,执行完后返回函数的头结点指针(也是NODE1型指针)
NODE* Creat(NODE *head);//创建无序链表
void Print(NODE* head);//打印(输出)该链表
NODE* Sort(NODE* head);//排序
void SortI(int *Sort, int num);//通用排序函数(插入法)
NODE* Search(NODE* head, int x);//查找数据为x的结点,若能找到,返回该结点的指针
NODE* Insert(NODE* head, NODE* s);//把s 指针指向的结点插入链表中
NODE* Delete(NODE* head, int x);//删除链表中数据为num的结点
NODE* Free(NODE* head);//链表的释放
int main() {
NODE* st,*head=NULL;
int num;
char c;
cout << "\n\tCreat a list:\n";
head = Initlist();
Creat(head);
while (1) {
cout << "What do you want to do?\n";
cout << "\n\tD:Delete I:Insert P:Print S:Search O:Sort E:Exit\n";
cin>>c;
switch (toupper(c)) {
case 'D':
cout << "Input a number to be deleted:";
cin >> num;
Delete(head, num);
break;
case 'I':
cout << "Input a number to be inserted:";
cin >> num;
st = new NODE;
st->data = num;
Insert(head, st);
break;
case 'P':
Print(head);
break;
case 'S':
cout << "Input a number to be searched:";
cin >> num;
if (Search(head, num) != NULL) {
cout << "It is in the list.\n";
}
else
cout << "It is not in the list.\n";
break;
case 'O':
Sort(head);
break;
case 'E':
Free(head);
exit(0);
}
}
return 0;
}