线性表特点:同一性,有穷性,有序性 线性表两种存储结构:顺序存储,链式存储 链式存储三种表:单链表,循环链表,双向链表 单链表基本运算:初始化,头插法建立,尾插法建立,按序号查找,按值查找,求长度,插入,删除,合并
#include<iostream>
#include<stack>
#include<cmath>
using namespace std;
typedef struct Node{
int data;
struct Node* next;
}Node,*LinkList;
void initList(LinkList* l) {
*l = (LinkList)malloc(sizeof(Node));
(*l)->next = NULL;
}
void insertListTail(LinkList l,int n) {
Node* p = l;
while (p->next != NULL) {
p = p->next;
}
Node* m = (Node*)malloc(sizeof(Node));
m->data = n;
p->next = m;
m->next = NULL;
}
void insertListHead(LinkList l, int n) {
Node* p = l;
Node* m = (Node*)malloc(sizeof(Node));
m->data = n;
m->next = l->next;
p->next = m;
}
int listLength(LinkList l) {
int j = 0;
Node* p = l;
while (p->next != NULL) {
p = p->next;
j++;
}
return j;
}
int getListValue(LinkList l, int pos) {
Node* p = l;
int j = 0;
while (p->next != NULL) {
j++;
p = p->next;
if (j == pos) {
return p->data;
}
}
return 0;
}
void insertListPos(LinkList l, int n, int pos) {
Node* p = l;
int j = 0;
while (p->next != NULL) {
j++;
p = p->next;
if (j == pos - 1) {
Node* m = (Node*)malloc(sizeof(Node));
m->data = n;
m->next = p->next;
p->next = m;
}
}
}
int findListValue(LinkList l, int n) {
Node* p = l;
int j = 0;
while (p->next != NULL) {
p = p->next;
j++;
if (p->data == n) {
return j;
}
}
return 0;
}
void delListAll(LinkList l) {
Node* p = l;
while (p->next != NULL) {
Node* m = p;
p = p->next;
free(m);
}
l->next = NULL;
}
LinkList mergeList(LinkList l1, LinkList l2) {
LinkList l;
initList(&l);
Node* p = l;
Node* t;
Node* p1 = l1;
Node* p2 = l2;
while (p1->next != NULL && p2->next != NULL) {
if (p1->next->data > p2->next->data) {
t = p1; p1 = p2; p2 = t;
}
t = p1->next->next;
p->next = p1->next;
p = p->next;
p1->next = t;
t = p2->next->next;
p->next = p2->next;
p = p->next;
p->next = NULL;
p2->next = t;
}
if (p1->next != NULL) {
p->next = p1->next;
}
if (p2->next != NULL) {
p->next = p2->next;
}
return l;
}
void coutList(LinkList l) {
if (l->next == NULL) {
cout << "LIST EMPTY!" << endl;
return;
}
Node* p = l;
while (p->next != NULL) {
p = p->next;
cout << p->data << " ";
}
cout << "------LIST COUT DONE!" << endl;
}
void delListPos(LinkList l, int pos) {
Node* p = l;
int j = 0;
while (p->next != NULL) {
if (j == pos - 1) {
Node* m = p->next;
p->next = m->next;
free(m);
}
p = p->next;
j++;
}
}
int main() {
LinkList l;
initList(&l);
for (int i = 0; i < 5; i++) {
insertListTail(l, i);
}
for (int i = 5; i < 10; i++) {
insertListHead(l, i);
}
insertListPos(l, 15, 5);
cout << "length: " << listLength(l) << endl;
coutList(l);
cout << "value[3]: " << getListValue(l, 3) << endl;
cout << "pos of 3: " << findListValue(l, 3) << endl;
cout << "delete pos 6,1" << endl;
delListPos(l, 1);
delListPos(l, 6);
coutList(l);
cout << "delete all" << endl;
delListAll(l);
coutList(l);
LinkList l1, l2;
initList(&l1);
initList(&l2);
insertListHead(l1, 4);
insertListHead(l1, 2);
insertListHead(l2, 3);
insertListHead(l2, 1);
insertListHead(l1, 0);
cout << "l1: ";
coutList(l1);
cout << "l2: ";
coutList(l2);
LinkList l3 = mergeList(l1, l2);
cout << "merge: ";
coutList(l3);
return 0;
}
|