标签:++ std 顺序表 等于 status empty lib merge pre
#include<stdio.h> #include<stdlib.h> #include<iostream> #define MAXSIZE 1000 #define ERROR 0 #define OK 1 using namespace std; typedef int Status; typedef int ElemType; /////////////线性表定义(顺序表)//////////////////////// typedef struct { ElemType *elem; int length; }SqList; /////////////线性表定义(单链表)//////////////////////// typedef struct LNode { ElemType data; struct LNode *next; }LNode,*LinkList; /////////////线性表初始化//////////////////////// Status InitList(SqList &L) { L.elem = new ElemType[MAXSIZE]; if(!L.elem) exit(ERROR); L.length = 0; return OK; } Status InitList(LinkList &L) { L = new LNode; L->next = NULL; return OK; } /////////////线性表销毁////////////////////////// Status DestoryList(SqList &L) { delete L.elem; L.elem = NULL; L.length = 0; return OK; } Status DestoryList(LinkList &L) { LinkList p; while(L) { p = L; L = L->next; delete p; } return OK; } /////////////线性表清空////////////////////////// Status ClearList(SqList &L) { L.length = 0; return OK; } Status ClearList(LinkList &L) { LinkList p; L = L->next; while(L) { p = L; L = L->next; delete p; } L->next = NULL; return OK; } /////////////线性表是否为空////////////////////// Status ListEmpty(SqList L) { if(L.length == 0) return OK; return ERROR; } Status ListEmpty(LinkList L) { if(L->next == NULL) return OK; return ERROR; } /////////////线性表长度////////////////////////// Status ListLength(SqList L) { return L.length; } Status ListLength(LinkList L) { LinkList p; int sum = 0; p = L->next; while(p) { p = p->next; sum++; } return sum; } /////////////获取线性表元素值//////////////////// Status GetElem(SqList L,int i,ElemType &e) { if(i<1 || i>L.length) return ERROR; e = L.elem[i-1]; return OK; } Status GetElem(LinkList L,int i,ElemType &e) { LinkList p; p = L->next; int j = 1; while(p&&j<i) { p=p->next; ++j; } if(!p||j>i) return ERROR; e = p->data; return OK; } /////////////获取线性表元素位置////////////////// int LocateElem(SqList L,ElemType e) { for(int i=0;i<L.length;i++) if(L.elem[i]==e) return i+1; return ERROR; } LNode *LocateElem(LinkList L,ElemType e) { LinkList p; p = L->next; while(p && p->data!=e) p = p->next; return p; } /////////////返回线性表前驱////////////////////// Status PriorElem(SqList L,ElemType cur_e,ElemType &pre_e) { int i = 0; while(L.elem[i]!=cur_e && i<L.length) i++; if(i == 0 || i == L.length) return ERROR; L.elem[i-1] = pre_e; return OK; } Status PriorElem(LinkList L,ElemType cur_e,ElemType &pre_e) { LinkList p,q; p = L->next; q = L; while(p->next) { q = p->next; if(q->data = cur_e) { pre_e = q->data; return OK; } p = q; } return ERROR; } /////////////返回线性表后继////////////////////// Status NextElem(SqList L,ElemType cur_e,ElemType &pre_e) { int i = 0; while(L.elem[i]!=cur_e && i<L.length) i++; if(i == 0 || i == L.length) return ERROR; pre_e = L.elem[i-1]; return OK; } Status NextElem(LinkList L,ElemType cur_e,ElemType &next_e) { LinkList p; p=L->next; while(p->next) { if(p->data == cur_e) { next_e = p->next->data; return OK; } p = p->next; } return ERROR; } /////////////线性表插入////////////////////////// Status ListInsert(SqList &L,int i,ElemType &e) { if((i<1)||(i>L.length)) return ERROR; //如果插入元素不在表的范围之内,插入失败 if(L.length == MAXSIZE) return ERROR; //如果顺序表已满,插入失败 for(int j = L.length;j >= i-1;j--) //从第i位开始,每个元素依次向后移动一个单位 { L.elem[j+1] = L.elem[j]; } L.elem[i-1] = e; //将第i位元素数据域设置为e ++L.length; //表长+1 return OK; } Status ListInsert(LinkList &L,int i,ElemType &e) { LinkList p,s; int j=0; p=L; while(p && (j<i-1)) //查找第i-1个结点,使p指向该结点 { p = p->next; ++j; } if(!p || j>i-1) return ERROR; //i>n+1或者i<1,插入失败 s = new LNode; //为新结点分配内存 s->data = e; //新结点数据域置为e s->next = p->next; //新结点的后继设置为p的后继 p->next = s; //p的后继设置为s return OK; } /////////////线性表删除////////////////////////// Status ListDelete(SqList &L,int i) { if((i<1) || (i>L.length)) return ERROR; //如果删除元素不在表的范围之内,删除失败 for(int j = i;j <= L.length;j++) L.elem[j-1] = L.elem[j]; //被删除元素之后的元素前移 --L.length; //表长-1 return OK; } Status ListDelete(LinkList &L,int i) { LinkList p,q;int j = 0; p = L; while((p->next) && (j<i-1)) //查找第i-1个结点,p指向该结点 { p = p->next; ++j; } if(!(p->next) || (j>i-1)) return ERROR; //当i>n或i<1时,删除位置不合适 q = p->next; //临时保存被删结点的位置以备释放 p->next = q->next; //改变删除结点前驱结点的指针域 delete q; //释放删除结点的空间 return OK; } /////////////线性表元素遍历////////////////////// Status TraverseList(SqList L) { for(int i=0;i<L.length;i++) { } return OK; } Status TraverseList(LinkList L) { while(L->next) { L = L->next; cout<<L->data<<‘ ‘; } return OK; } /////////////创建单链表(头插法)(带头节点)////////////////////// void CreateList_H(LinkList &L,int n) { LinkList p; L = new LNode; L->next = NULL; for(int i = 0;i < n;i++) { p = new LNode; cin>>p->data; p->next = L->next; L->next = p; } } /////////////创建单链表(头插法)(不带头节点)////////////////////// void CreateList_NH(LinkList &L,int n) { LinkList p; L->next = NULL; for(int i = 0;i < n;i++) { p = new LNode; cin>>p->data; p->next = L->next; L->next = p; } } /////////////创建单链表(尾插法)(带头节点)////////////////////// void CreateList_R(LinkList &L,int n) { LinkList p,r; L = new LNode; L->next = NULL; r=L; for(int i = 0;i < n;i++) { p = new LNode; cin>> p->data; p->next = NULL; r->next = p; r = p; } } /////////////创建单链表(尾插法)(不带头节点)////////////////////// void CreateList_NR(LinkList &L,int n) { LinkList p,r; L->next = NULL; r=L; for(int i = 0;i < n;i++) { p = new LNode; cin>> p->data; p->next = NULL; r->next = p; r = p; } } /////////////双向链表定义////////////////////// typedef struct DuLNode { ElemType data; //数据域 struct DuLNode *prior; //指向直接前驱 struct DuLNode *next; //指向直接后继 }DuLNode,*DuLinkList; /////////////双向链表插入////////////////////// Status ListInsert_DuL(DuLinkList &L,int i,ElemType e) { DuLinkList p,s; int j=0; p=L; while(p && (j<i-1)) //查找第i-1个结点,使p指向该结点 { p = p->next; ++j; } if(!p || j>i-1) return ERROR; //i>n+1或者i<1,插入失败 s = new DuLNode; s->data = e; //s的数据域设置为e s->prior = p->prior; //s的前驱设置为p的前驱 p->prior->next = s; //p的前驱元素的后继设置为s s->next = p; //s的后继设置为p p->prior = s; //p的前驱设置为s return OK; } /////////////双向链表删除////////////////////// Status ListDelete_DuL(DuLinkList &L,int i) { DuLinkList p,q;int j = 0; p = L; while((p->next) && (j<i-1)) //查找第i-1个结点,p指向该结点 { p = p->next; ++j; } if(!(p->next) || (j>i-1)) return ERROR; //当i>n或i<1时,删除位置不合适 p->prior->next = p->next; //p的前驱元素的后继设置为p的后继 p->next->prior = p->prior; //p的后继元素的前驱设置为p的前驱 delete p; //删除元素p return OK; } /////////////线性表的合并(顺序表)(无序表)////////////////////// void MergeList(SqList &LA,SqList LB) { int m = LA.length; int n = LB.length; ElemType e; for(int i = 1;i<=n;i++) { GetElem(LB,i,e); if(!LocateElem(LA,e)) ListInsert(LA,++m,e); } } /////////////线性表的合并(顺序表)(有序表)////////////////////// void MergeList_Sq(SqList LA,SqList LB,SqList &LC) { //将AB两表中的有序数据统一有序放到一个C表中 LC.length = LB.length+LA.length;//新表的长度等于两表的长度和 LC.elem = new ElemType[LC.length];//为新表申请空间 ElemType *pa,*pb,*pc,*pa_last,*pb_last;//定位三个表中各个结点的位置 pc = LC.elem; pa = LA.elem; pb = LB.elem; pa_last = LA.elem + LA.length-1; pb_last = LB.elem + LB.length-1; while((pa<pa_last)&&(pb<pb_last))//依次摘取两表中数值较小的结点放到c表的最后 { if(*pa<=*pb) *pc++ = *pa++;//如果当前A表的数据小于B表的数据,先取A表数据 else *pc++ = *pb++; //否则取B表的数据 } while(pa <= pa_last) *pc++ = *pa++;//如果A表的数据还没有取完,继续取 while(pb <= pb_last) *pc++ = *pb++;//如果B表的数据还没有取完,继续取 } /////////////线性表的合并(链式表)////////////////////// void MergeList_L(LinkList &LA,LinkList &LB,LinkList &LC) { LinkList pa,pb,pc; pa = LA->next; pb = LB->next; LC = LA; pc = LC; while(pa && pb) { if(pa->next <= pb->next) { pc->next = pa; pc = pa; pa = pa->next; } else { pc->next = pb; pc = pb; pb = pb->next; } } pc->next = pa? pa:pb; delete LB; }
标签:++ std 顺序表 等于 status empty lib merge pre
原文地址:https://www.cnblogs.com/basketball616/p/11143597.html