#include<iostream> #include<string.h> #include<map> #include<queue> #include<unordered_map> using namespace std; #pragmawarning(disable:4996)
#define MaxSize 50 typedefint ElemType;
typedefstruct { ElemType data[MaxSize];//元素 int length;//长度 }SqList;
02 建立与初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*建立顺序表*/ voidCreateList(SqList*& L, ElemType a[], int n) { int i = 0, k = 0;//k表示元素个数 L = (SqList*)malloc(sizeof(SqList)); while (i < n) { L->data[i] = a[i]; k++; i++; } L->length = k; }
voidInitList(SqList*& L) { L = (SqList*)malloc(sizeof(SqList)); L->length = 0; }
/*打印线性表*/ voidDispList(SqList* L) { for (int i = 0; i < L->length; i++) printf("%d ", L->data[i]); printf("\n"); }
04 得到第 i 的元素
1 2 3 4 5 6 7
/*得到第i的元素*/ boolGetElem(SqList* L, int i, ElemType& e) { if (i<1 || i>L->length) returnfalse; e = L->data[i - 1]; returntrue; }
05 返回元素 e 的下标
1 2 3 4 5 6 7 8
/*返回元素e的下标*/ intLocateElem(SqList* L, ElemType e) { int i = 0; while (i < L->length&& L->data[i] != e) i++; if (i >= L->length)return0; elsereturn i + 1; }
06 在第 i 个位置插入元素
1 2 3 4 5 6 7 8 9 10 11
/*在第i个位置插入元素*/ boolListInsert(SqList*& L, int i, ElemType e) { int j; if (i<1 || i>L->length + 1)returnfalse; i--; for (j = L->length; j > i; j--) L->data[j] = L->data[j - 1]; L->data[i] = e; L->length++; returntrue; }
07 删除第 i 个元素
1 2 3 4 5 6 7 8 9 10
/*删除第i个元素*/ boolListDelete(SqList*& L, int i, ElemType e) { int j; if (i<1 || i>L->length)returnfalse; i--; e = L->data[i]; for (j = i; j < L->length - 1; j++)L->data[j] = L->data[j + 1]; L->length--; returntrue; }
typedefstruct { ElemType data[MaxSize];//元素 int length;//长度 }SqList;
/*建立顺序表*/ voidCreateList(SqList*& L, ElemType a[], int n){ int i = 0, k = 0;//k表示元素个数 L = (SqList*)malloc(sizeof(SqList)); while (i < n) { L->data[i] = a[i]; k++; i++; } L->length = k; }
voidInitList(SqList*& L){ L = (SqList*)malloc(sizeof(SqList)); L->length = 0; }
/*销毁线性表*/ voidDestroyList(SqList*& L){ free(L); }
/*是否为空*/ boolListEmpty(SqList* L){ return (L->length == 0); } /*返回长度*/ intListLength(SqList* L){ return (L->length); } /*打印线性表*/ voidDispList(SqList* L){ for (int i = 0; i < L->length; i++) printf("%d ", L->data[i]); printf("\n");
} /*得到第i的元素*/ boolGetElem(SqList* L, int i, ElemType& e){ if (i<1 || i>L->length) returnfalse; e = L->data[i - 1]; returntrue; }
/*返回元素e的下标*/ intLocateElem(SqList* L, ElemType e){ int i = 0; while (i < L->length&& L->data[i] != e) i++; if (i >= L->length)return0; elsereturn i + 1; }
/*在第i个位置插入元素*/ boolListInsert(SqList*& L, int i, ElemType e){ int j; if (i<1 || i>L->length + 1)returnfalse; i--; for (j = L->length; j > i; j--) L->data[j] = L->data[j - 1]; L->data[i] = e; L->length++; returntrue; }
/*删除第i个元素*/ boolListDelete(SqList*& L, int i, ElemType e){ int j; if (i<1 || i>L->length)returnfalse; i--; e = L->data[i]; for (j = i; j < L->length - 1; j++)L->data[j] = L->data[j + 1]; L->length--; returntrue; }
/*求长度*/ intListLength(LinkNode *L){ int n = 0; LinkNode* p = L; while (p->next!=NULL) { n++; p = p->next; } return n; }
/*打印单链表*/ voidDispList(LinkNode* L){ LinkNode* p = L->next; while (p!= NULL) { printf("%d ",p->data); p = p->next; } printf("\n"); }
/*求第i个位置的元素值*/ boolGetElem(LinkNode *L,int i,ElemType &e){ int j = 0; LinkNode* p = L; if (i <= 0) returnfalse; while (j < i && p != NULL) {//移动到位置i j++; p = p->next; } if (p == NULL)returnfalse; else { e = p->data;//得到 returntrue; } }
/*按元素值查找,得到下标*/ intLocateElem(LinkNode *L,ElemType e){ int i = 1; LinkNode* p = L->next; while (p!=NULL && p->data!=e) {//移到对应元素值 p = p->next; i++; } if (p == NULL)return0; elsereturn i;
}
/*在第i个位置插入元素e*/ boolListInsert(LinkNode *&L,int i,ElemType e){ int j = 0; LinkNode* p = L, * s; if (i <= 0)returnfalse; while (j <i-1 && p!=NULL) { j++; p = p->next; } if (p==NULL) returnfalse; else { s = (LinkNode*)malloc(sizeof(LinkNode)); s->data = e; s->next = p->next;//头插法 p->next = s; returntrue; } }
/*删除单链表下标i的元素,并返回*/ boolListDelete(LinkNode*& L, int i, ElemType e){ int j = 0; LinkNode* p = L, * q; if (i <= 0)returnfalse; while (j < i - 1 && p != NULL) { j++;//移动指针 p = p->next; } if (p == NULL) returnfalse; else { q = p->next; if (q == NULL)returnfalse; e = q->data; p->next = q->next;//p->next=p->next->next; free(q); returntrue; } }
LinkNode* getMin(LinkNode*& L){ LinkNode* min = L, * head = L; while (head->next) { if (min->data > (head->next->data)) min = head->next; head = head->next; } return min; }
voidSortList(LinkNode*& L){ LinkNode* j, * i = L->next; int temp; for (; i->next != NULL; i = i->next) { j = getMin(i); if (i->data != j->data) { temp = i->data; i->data = j->data; j->data = temp; } } }
/*头插法*/ voidCreateListF(DLinkNode*&L,ElemType a[],int n){ DLinkNode* s; L = (DLinkNode*)malloc(sizeof(DLinkNode)); L->prior = L->next = NULL;//前后指针域置为null for (int i = 0; i < n;i++) {//循环建立数据节点s s = (DLinkNode*)malloc(sizeof(DLinkNode)); s->data = a[i];//数据存入 s->next = L->next;//将节点s插入到原首节点之前、头节点之后 if (L->next!=NULL) {//若L存在数据节点,修改L->next的前驱指针 L->next->prior = s; } L->next = s; s->prior = L; } }
/*尾插法*/ voidCreateListR(DLinkNode*& L, ElemType a[], int n){ DLinkNode* s,*r; L = (DLinkNode*)malloc(sizeof(DLinkNode));//创建头节点 r = L;//r始终指向尾节点,初始是指向头节点 for (int i = 0; i < n; i++) {//循环建立数据节点 s = (DLinkNode*)malloc(sizeof(DLinkNode)); s->data = a[i];//存入 r->next = s;//将节点s插入到节点r之后,r指向尾节点 s->prior = r; r = s;//尾巴后面插元素,然后尾巴移到后一个元素,保证r始终是尾巴 } r->next = NULL;//为节点的next置为null }
/*在第i个位置插入元素e*/ boolListInsert(DLinkNode*& L, int i, ElemType e){ int j = 0; DLinkNode* p = L, * s; //p指向头节点,j设置为0 if (i <= 0)returnfalse; //i错误返回false while (j < i - 1 && p != NULL) {//查找第i-1个节点 j++; p = p->next; } if (p == NULL)//未找到第i-1个节点 returnfalse; else { s = (DLinkNode*)malloc(sizeof(DLinkNode)); s->data = e; s->next = p->next;//头插法,在p节点之后插入s if (p->next != NULL) //若p节点存在后继节点,修改其前驱指针 p->next->prior = s; s->prior = p; p->next = s; returntrue; } }
/*删除双链表下标i的元素,并返回*/ boolListDelete(DLinkNode*& L, int i, ElemType e){ int j = 0; DLinkNode* p = L, * q; if (i <= 0)returnfalse; while (j < i - 1 && p != NULL) { j++;//移动指针 p = p->next; } if (p == NULL) returnfalse; else { q = p->next; if (q == NULL)returnfalse; e = q->data; p->next = q->next;//p->next=p->next->next; if (p->next != NULL)p->next->prior = p; free(q); returntrue; } }
/*初始化线性表*/ voidInitList(DLinkNode*&L){ L = (DLinkNode*)malloc(sizeof(DLinkNode)); L->next = NULL;//指向null }
/*销毁线性表*/ voidDestroyList(DLinkNode*&L){ DLinkNode* pre = L, * p = L->next; while(p!=NULL){ free(pre);//销毁节点,从头结点开始 pre = p; p = pre->next; } free(pre); }
/*求长度*/ intListLength(DLinkNode*L){ int n = 0; DLinkNode* p = L; while (p->next!=NULL) { n++; p = p->next; } return n; }
/*打印单链表*/ voidDispList(DLinkNode* L){ DLinkNode* p = L->next; while (p!= NULL) { printf("%d ",p->data); p = p->next; } printf("\n"); }
/*求第i个位置的元素值*/ boolGetElem(DLinkNode*L,int i,ElemType &e){ int j = 0; DLinkNode* p = L; if (i <= 0) returnfalse; while (j < i && p != NULL) {//移动到位置i j++; p = p->next; } if (p == NULL)returnfalse; else { e = p->data;//得到 returntrue; } }
/*按元素值查找,得到下标*/ intLocateElem(DLinkNode*L,ElemType e){ int i = 1; DLinkNode* p = L->next; while (p!=NULL && p->data!=e) {//移到对应元素值 p = p->next; i++; } if (p == NULL)return0; elsereturn i; }