// replace element of position index, so original element will toward back bool ListInsert(pListHeader plh, unsigned int index, pListNode pNode) { if(index > plh->ListSize) ErrorFunc("Too much bigger index!\n", OUT_OF_SCOPE); pListNode pRplcedNode = GetElem(plh, index); pListNode pPriorNode = PriorElem(plh, pRplcedNode); pNode->next = pPriorNode->next; pPriorNode->next = pNode; (plh->ListSize)++; return true; }
int main() { SqList L; printf("%ld\n",sizeof(L)); InitSqList(L); int i; for(i=0;i<20;++i) push_back(L,i); Display(L); printf("L.length :%d\n",L.length); printf("listlength:%d\n",ListLength(L)); Elem e,prio,next; GetElem(L,2,e); PriorElem(L,prio,e); NextElem(L,next,e); printf("the number 2 tumple:%d\n",e); printf("prio e:%d\n",prio); printf("next e:%d\n",next); ListInsert(L,8,10000); Display(L); LocateList(L,10000); DeleteK(L,5,3); Display(L); DestroyList(L); printf("size of a.length :%d\n",L.listsize); if(ListEmpty(L)) printf("empty!\n"); /*char* p = NULL; p=(char*)malloc(10*sizeof(char)); if(p==NULL) { printf("filure"); exit(1); } printf("%p\n",p); strcpy(p,"abc"); printf("%c\n%c\n%c\n",*p,*p+1,*p+2); printf("%p\n%p\n",p,p+1); //if(p!=NULL) free(p); //p=NULL; //DestroyList(L); //Display(L);*/ return 0; }
void ListDelete(pListHeader plh, unsigned int index) { pListNode pNode = GetElem(plh, index); pListNode tmp = pNode; if(index == 1) { plh->next = pNode->next; } else { pListNode pPriorNode = PriorElem(plh, pNode); pPriorNode->next = pNode->next; } free(tmp); (plh->ListSize)--; }
void main() { LinkList L; ElemType e; int j; Status i; InitList(L); i = ListEmpty(L); printf("L是否空 i = %d (1:空 0:否)\n", i); ListInsert(L, 1, 3); ListInsert(L, 2, 5); i = GetElem(L, 1, e); j = ListLength(L); printf("L中的数据元素个数=%d, 第一个数据元素的值为%d.\n", j, e); printf("L中的数据元素依次为:"); ListTraverse(L, print); PriorElem(L, 5, e); printf("5前面的元素的值为%d.\n", e); NextElem(L, 3, e); printf("3后面的元素的值为%d.\n", e); printf("L是否空 %d(1:空 0:否)\n", ListEmpty(L)); j = LocateElem(L, 5, equal); if ( j ) printf("L的第%d个元素为5.\n", j); else printf("不存在值为5的元素.\n"); i = ListDelete(L, 2, e); printf("删除L的第2个元素:\n"); if ( i ) { printf("删除的元素值为%d, 现在L中的数据元素依次为", e); ListTraverse(L, print); } else printf("删除不成功!\n"); ClearList(L); printf("清空L后, L是否空:%d (1:空 0:否)\n", ListEmpty(L)); DestroyList(L); }
void main() /* 除了几个输出语句外,主程和main2-1.c很像 */ { LinkList L; /* 与main2-1.c不同 */ ElemType e,e0; Status i; int j,k; i=InitList(&L); for(j=1;j<=5;j++) i=ListInsert(L,1,j); printf("在L的表头依次插入1~5后:L="); ListTraverse(L,visit); /* 依次对元素调用visit(),输出元素的值 */ i=ListEmpty(L); printf("L是否空:i=%d(1:是 0:否)\n",i); i=ClearList(L); printf("清空L后:L="); ListTraverse(L,visit); i=ListEmpty(L); printf("L是否空:i=%d(1:是 0:否)\n",i); for(j=1;j<=10;j++) ListInsert(L,j,j); printf("在L的表尾依次插入1~10后:L="); ListTraverse(L,visit); GetElem(L,5,&e); printf("第5个元素的值为:%d\n",e); for(j=0;j<=1;j++) { k=LocateElem(L,j,comp); if(k) printf("第%d个元素的值为%d\n",k,j); else printf("没有值为%d的元素\n",j); } for(j=1;j<=2;j++) /* 测试头两个数据 */ { GetElem(L,j,&e0); /* 把第j个数据赋给e0 */ i=PriorElem(L,e0,&e); /* 求e0的前驱 */ if(i==INFEASIBLE) printf("元素%d无前驱\n",e0); else printf("元素%d的前驱为:%d\n",e0,e); } for(j=ListLength(L)-1;j<=ListLength(L);j++)/*最后两个数据 */ { GetElem(L,j,&e0); /* 把第j个数据赋给e0 */ i=NextElem(L,e0,&e); /* 求e0的后继 */ if(i==INFEASIBLE) printf("元素%d无后继\n",e0); else printf("元素%d的后继为:%d\n",e0,e); } k=ListLength(L); /* k为表长 */ for(j=k+1;j>=k;j--) { i=ListDelete(L,j,&e); /* 删除第j个数据 */ if(i==ERROR) printf("删除第%d个数据失败\n",j); else printf("删除的元素为:%d\n",e); } printf("依次输出L的元素:"); ListTraverse(L,visit); DestroyList(&L); printf("销毁L后:L=%u\n",L); }
void main() { SqList L; ElemType e, e0; Status i; int j, k; i = InitList(&L); printf("初始化L后:L.elem=%u L.length=%d L.listsize=%d\n", L.elem, L.length, L.listsize); for (j = 1; j <= 5; j++) i = ListInsert(&L, 1, j); printf("在L的表头依次插入1~5后:*L.elem="); for (j = 1; j <= 5; j++) printf("%d ", *(L.elem + j - 1)); printf("\n"); printf("L.elem=%u L.length=%d L.listsize=%d\n", L.elem, L.length, L.listsize); i = ListEmpty(L); printf("L是否空:i=%d(1:是 0:否)\n", i); i = ClearList(&L); printf("清空L后:L.elem=%u L.length=%d L.listsize=%d\n", L.elem, L.length, L.listsize); i = ListEmpty(L); printf("L是否空:i=%d(1:是 0:否)\n", i); for (j = 1; j <= 10; j++) ListInsert(&L, j, j); printf("在L的表尾依次插入1~10后:*L.elem="); for (j = 1; j <= 10; j++) printf("%d ", *(L.elem + j - 1)); printf("\n"); printf("L.elem=%u L.length=%d L.listsize=%d\n", L.elem, L.length, L.listsize); ListInsert(&L, 1, 0); printf("在L的表头插入0后:*L.elem="); for (j = 1; j <= ListLength(L); j++) /* ListLength(L)为元素个数 */ printf("%d ", *(L.elem + j - 1)); printf("\n"); printf("L.elem=%u(有可能改变) L.length=%d(改变) L.listsize=%d(改变)\n", L.elem, L.length, L.listsize); GetElem(L, 5, &e); printf("第5个元素的值为:%d\n", e); for (j = 3; j <= 4; j++) { k = LocateElem(L, j, comp); if (k) printf("第%d个元素的值为%d的平方\n", k, j); else printf("没有值为%d的平方的元素\n", j); } for (j = 1; j <= 2; j++) /* 测试头两个数据 */ { GetElem(L, j, &e0); /* 把第j个数据赋给e0 */ i = PriorElem(L, e0, &e); /* 求e0的前驱 */ if (i == INFEASIBLE) printf("元素%d无前驱\n", e0); else printf("元素%d的前驱为:%d\n", e0, e); } for (j = ListLength(L) - 1; j <= ListLength(L); j++) /* 最后两个数据 */ { GetElem(L, j, &e0); /* 把第j个数据赋给e0 */ i = NextElem(L, e0, &e); /* 求e0的后继 */ if (i == INFEASIBLE) printf("元素%d无后继\n", e0); else printf("元素%d的后继为:%d\n", e0, e); } k = ListLength(L); /* k为表长 */ for (j = k + 1; j >= k; j--) { i = ListDelete(&L, j, &e); /* 删除第j个数据 */ if (i == ERROR) printf("删除第%d个数据失败\n", j); else printf("删除的元素值为:%d\n", e); } printf("依次输出L的元素:"); ListTraverse(L, visit); /* 依次对元素调用visit(),输出元素的值 */ printf("L的元素值加倍后:"); ListTraverse(L, dbl); /* 依次对元素调用dbl(),元素值乘2 */ ListTraverse(L, visit); DestroyList(&L); printf("销毁L后:L.elem=%u L.length=%d L.listsize=%d\n", L.elem, L.length, L.listsize); }
void main() { int j,k; Status i; ElemType e,e0; SLinkList L; InitList(L); for(j=1;j<=5;j++) i=ListInsert(L,1,j); printf("在L的表头依次插入1~5后:L="); ListTraverse(L,visit); i=ListEmpty(L); printf("L是否空:i=%d(1:是 0:否)表L的长度=%d\n",i,ListLength(L)); i=ClearList(L); printf("清空L后:L="); ListTraverse(L,visit); i=ListEmpty(L); printf("L是否空:i=%d(1:是 0:否)表L的长度=%d\n",i,ListLength(L)); for(j=1;j<=10;j++) ListInsert(L,j,j); printf("在L的表尾依次插入1~10后:L="); ListTraverse(L,visit); GetElem(L,5,&e); printf("第5个元素的值为:%d\n",e); for(j=0;j<=1;j++) { k=LocateElem(L,j); if(k) printf("值为%d的元素在静态链表中的位序为%d\n",j,k); else printf("没有值为%d的元素\n",j); } for(j=1;j<=2;j++) /* 测试头两个数据 */ { GetElem(L,j,&e0); /* 把第j个数据赋给e0 */ i=PriorElem(L,e0,&e); /* 求e0的前驱 */ if(!i) printf("元素%d无前驱\n",e0); else printf("元素%d的前驱为:%d\n",e0,e); } for(j=ListLength(L)-1;j<=ListLength(L);j++) /* 最后两个数据 */ { GetElem(L,j,&e0); /* 把第j个数据赋给e0 */ i=NextElem(L,e0,&e); /* 求e0的后继 */ if(!i) printf("元素%d无后继\n",e0); else printf("元素%d的后继为:%d\n",e0,e); } k=ListLength(L); /* k为表长 */ for(j=k+1;j>=k;j--) { i=ListDelete(L,j,&e); /* 删除第j个数据 */ if(i) printf("删除的元素为:%d\n",e); else printf("删除第%d个数据失败\n",j); } printf("依次输出L的元素:"); ListTraverse(L,visit); /* 依次对元素调用visit(),输出元素的值 */ }
int main() { Sqlist L; ElemType e, e0; Status i; int j, k; InitList(L); printf("初始化后:L.elem = %d, L.length = %d, L.listsize = %d\n", L.elem, L.length, L.listsize); for ( j = 1; j <= 5; j++ ) i = ListInsert_before(L, 1, j); printf("在L的表头依次插入1~5后:"); for ( j = 1; j <= 5; j++ ) printf("%d, ", *(L.elem + j - 1)); printf("\n"); printf("插入后:L.elem = %d, L.length = %d, L.listsize = %d\n", L.elem, L.length, L.listsize); i = ListEmpty(L); printf("L是否空:i = %d(1:是 0:否)\n", i); ClearList(L); printf("清空L后:L.elem = %d, L.length = %d, L.listsize = %d\n", L.elem, L.length, L.listsize); for ( j = 1; j <= 10; j++ ) i = ListInsert_before(L, j, j); printf("在L的表尾依次插入1~10后:"); for ( j = 1; j <= 10; j++ ) printf("%d, ", *(L.elem + j - 1)); printf("\n"); printf("插入后:L.elem = %d, L.length = %d, L.listsize = %d\n", L.elem, L.length, L.listsize); ListInsert_before(L, 1, 0); printf("在L的表头插入0后:"); for ( j = 1; j <= ListLength(L); j++ ) printf("%d, ", *(L.elem + j - 1)); printf("\n"); printf("插入后:L.elem = %d, L.length = %d, L.listsize = %d\n", L.elem, L.length, L.listsize); GetElem(L, 5, e); printf("第5个元素的值为:%d\n", e); for ( j = 10; j <= 11; j++ ) { k = LocateElem(L, j, equal); if ( k ) printf("第%d个元素的值为%d\n", k, j); else printf("没有值为%d的元素\n", j); } for ( j = 3; j <= 4; j++ ) { k = LocateElem(L, j, sq); if ( k ) printf("第%d个元素的值为%d的平方\n", k, j); else printf("没有值为%d的平方的元素\n", j); } for ( j = 1; j <= 2; j++ ) { GetElem(L, j, e0); i = PriorElem(L, e0, e); if ( i == INFEASIBLE ) printf("元素%d无前驱\n", e0); else printf("元素%d的前驱为:", e0, e); } for ( j = ListLength(L)-1; j <=ListLength(L); j++ ) { GetElem(L, j, e0); i = NextElem(L, e0, e); if ( i == INFEASIBLE ) printf("元素%d无后继\n", e0); else printf("元素%d的后继为%d\n", e0, e); } k = ListLength(L); for ( j = k+1; j >=k; j-- ) { i = ListDelete(L, j, e); if ( i == ERROR ) printf("删除第%d个元素失败\n", j); else printf("删除第%d个元素成功,其值为%d\n", j, e); } printf("依次输出L的元素:"); ListTraverse(L, print1); printf("加倍后:"); ListTraverse(L, dbl); ListTraverse(L, print1); DestroyList(L); printf("销毁后:L.elem = %d, L.length = %d, L.listsize = %d\n", L.elem, L.length, L.listsize); return 0; }
//expand void test2() { SLinkList L; SLinkList Lk; ElemType e; int i=0; int N=10; int n; InitSpace(L); InitList(L,n); printf("表是否为空(1:非空,0:空),结果:%d\n",!ListEmpty(L,n)); printf("表长%d\n",ListLength(L,n)); for(i=1;i<=5;i++) { ListInsert(L,n,i,2*i); } printf("插入5个元素(2,4,6,8,10),结果:"); TraverseList(L,n,visit); printf("\n"); i=3; if(PriorElem(L,n,3,e)) printf("%d的前驱为%d\n",i,e); if(NextElem(L,n,3,e)) printf("%d的后继为%d\n",i,e); for(i=1;i<=5;i++) { ListInsert(L,n,2*i-1,2*i-1); } printf("插入5个元素(1,3,5,7,9)后结果:"); TraverseList(L,n,visit); printf("\n"); printf("表是否为空(1:非空,0:空),结果:%d\n",!ListEmpty(L,n)); printf("表长%d\n",ListLength(L,n)); for(i=9;i<=10;i++) { printf("删除%d个元素为",i); if(ListDelete(L,n,i,e)) printf("%d\n",e); else printf("删除失败\n"); } int j=0; for(i=-1;i<=1;i++) { printf("查找%d所在位置",i); j=LocateElem(L,n,i,compare); if(j) printf("%d\n",j); else printf("查找失败\n"); } for(i=-1;i<=N+5;i+=3) { if(PriorElem(L,n,i,j)) printf("查找到%d的前驱%d\n",i,j); else printf("查找%d的前驱失败\n",i); if(NextElem(L,n,i,j)) printf("查找到%d的后继%d\n",i,j); else printf("查找%d的后继失败\n",i); } j=ClearList(L,n); if(j) printf("清空链表成功"); else printf("清空失败"); printf("表是否为空(1:非空,0:空),结果:%d\n",!ListEmpty(L,n)); printf("表长%d\n",ListLength(L,n)); MergeList2(Lk); printf("\n"); }
int main(int argc, const char * argv[]) { //线性表的顺序表示 SqList L; ElemType e; ElemType pre_e; ElemType next_e; InitList(L); if(ListEmpty(L)) { printf("empty\n"); }//if for(int i=0;i<30;i++){ e = i+1; ListInsert(L, i+1, e); } e = 15; printf("15所在的位置为: %d\n", LocateElem(L, 15, equal)); PriorElem(L, e, pre_e); NextElem(L, e, next_e); printf("e的前驱为:%d\n", pre_e); printf("e的后驱为:%d\n", next_e); GetElem(L, 22, e); printf("第22个数为:%d\n", e); printf("遍历:"); ListTraverse(L, visit); printf("\n"); printf("List length is:%d\n", ListLength(L)); ClearList(L); printf("清空\n"); printf("List length is:%d\n", ListLength(L)); if(ListEmpty(L)) { printf("empty\n"); } ListInsert(L, 1, 3); ListInsert(L, 2, 7); ListInsert(L, 3, 9); ListInsert(L, 4, 1); ListInsert(L, 5, 44); printf("List length is:%d\n", ListLength(L)); printf("遍历:"); ListTraverse(L, visit); printf("\n"); ListDelete(L, 3, e); printf("所删除的值为: %d\n", e); printf("遍历:"); ListTraverse(L, visit); printf("\n"); printf("xiaohui:\n"); DestroyList(L); system("pause"); return 0; }
/*------------------------------------------------------*/ int main(void) { SqList L; char getChoose; int choose = 1; /*char *p; scanf_s("%s", p);*/ while (choose) { char key; printf("是否需要对文件进行操作? Y or N\n"); key = getchar(); getchar(); if (key == 'Y' || key == 'y') key1 = 1; else key1 = 0; int op = 1, i = 0; ElemType cur_e = 0, prev_e = 0, next_e = 0, e = 0; while (op) { system("cls"); printf("\n\n"); printf(" Menu for Linear Table On Sequence Structure \n"); printf("------------------------------------------------------\n"); printf(" 1. IntiaList 7. LocateElem\n"); printf(" 2. DestroyList 8. PriorElem\n"); printf(" 3. ClearList 9. NextElem \n"); printf(" 4. ListEmpty 10. ListInsert\n"); printf(" 5. ListLength 11. ListDelete\n"); printf(" 6. GetElem 12. ListTrabverse\n"); printf(" 0. Exit\n"); printf("------------------------------------------------------\n"); printf(" 请选择你的操作[0~12]:"); scanf_s("%d", &op); switch (op) { case 1: //printf("\n----IntiaList功能待实现!\n"); if (key1) { if (IntiaList_file(L) == OK) { printf("文件加载创建成功!\n"); if (!L.elem) printf("线性表创建失败\n"); else { printf("线性表创建成功\n"); } } else printf("文件加载失败!\n"); getchar(); getchar(); break; } else { if (IntiaList(L) == OK) printf("线性表创建成功!\n"); else printf("线性表创建失败!\n"); getchar(); getchar(); break; } case 2: //printf("\n----DestroyList功能待实现!\n"); if (DestroyList(L) == OK) printf("线性表销毁成功!\n"); else printf("线性表销毁失败!\n"); getchar(); getchar(); break; case 3: if (ClearList(L) == OK) printf("线性表清空成功!\n"); //printf("\n----ClearList功能待实现!\n"); else printf("线性表清空失败!\n"); getchar(); getchar(); break; case 4: //printf("\n----ListEmpty功能待实现!\n"); if (ListEmpty(L) == OK) printf("线性表为空!\n"); else printf("线性表不为空!\n"); getchar(); getchar(); break; case 5: //printf("\n----ListLength功能待实现!\n"); printf("线性表的长度为%d\n", ListLength(L)); getchar(); getchar(); break; case 6: //printf("\n----GetElem功能待实现!\n"); getchar(); printf("请输入线性表需要查询的位置的下标:\n"); scanf_s("%d", &i); getchar(); if (GetElem(L, i, e) == OK) { printf("查询成功!\n"); printf("查询结果为%d\n", e); } else printf("查询失败!\n"); getchar(); break; case 7: getchar(); //printf("\n----LocateElem功能待实现!\n"); printf("请输入要查询的数:\n"); scanf_s("%d", &i); if (LocateElem(L, i) == OK) printf("查询成功!\n"); else printf("查询失败!\n"); getchar(); getchar(); break; case 8: //printf("\n----PriorElem功能待实现!\n"); getchar(); printf("请输入你想要查询前一个数的当前数:\n"); scanf_s("%d", &cur_e); getchar(); if (PriorElem(L, cur_e, prev_e) == OK) printf("查询结果为:%d\n", prev_e); else printf("查询失败!\n"); getchar(); break; case 9: //printf("\n----NextElem功能待实现!\n"); getchar(); printf("请输入你想要查询前一个数的当前数:\n"); scanf_s("%d", &cur_e); if (NextElem(L, cur_e, next_e) == OK) printf("查询结果为:%d\n", next_e); else printf("查询失败!\n"); getchar(); getchar(); break; case 10: //printf("\n----ListInsert功能待实现!\n"); getchar(); printf("请输入你想要插入的数据:\n"); scanf_s("%d", &e); printf("请输入你想要插入数据的位置:\n"); scanf_s("%d", &i); int h; h = ListInsert(L, i, e); if (h == OK) printf("插入成功!\n"); else if (h == ERROR) printf("插入失败!\n"); else printf("分配空间失败\n"); getchar(); getchar(); break; case 11: getchar(); //printf("\n----ListDelete功能待实现!\n"); printf("请输入你想要删除的数的位置:\n"); int i11; scanf_s("%d", &i11); if (ListDelete(L, i11, e) == OK) { printf("删除成功!\n删除的数字为%d\n", e); } else printf("删除失败!\n"); getchar(); getchar(); break; case 12: //printf("\n----ListTrabverse功能待实现!\n"); if (L.length == 0) printf("线性表为空表\n"); if (ListTrabverse(L) == ERROR) printf("线性表未初始化!\n"); getchar(); getchar(); break; case 0: printf("是否需要保持数据?Y or N\n"); getchar(); char save_or_not; save_or_not = getchar(); getchar(); if (save_or_not == 'Y' || save_or_not == 'y') { if (saveFile(L) == OK) printf("保存成功!\n"); else printf("保存失败!\n"); } break; }//end of switch }//end of while printf("是否继续使用此系统?Y or N\n"); getChoose = getchar(); getchar(); if (getChoose == 'N' || getChoose == 'n') break; } printf("欢迎下次再使用本系统!\n"); return 0; }//end of main()