两个顺序线性表的合并,关于数组指针的操作。
来源:互联网 发布:网站seo公司哪家专业 编辑:程序博客网 时间:2024/05/30 23:07
线性表的顺序存储结构,重点在两个线性表的合并。
对于数组可以使用指针也可以使用带下标的数组来表示每个元素。
#include <stdio.h>#include <conio.h>#include <stdlib.h>#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define OVERFLOW -1#define OK 1#define ERROR 0typedef int Elemtype;typedef int Status;typedef struct { Elemtype *elem; int length; int listsize;}SqList;//线性表的动态分配顺序存储结构Status InitList(SqList *L){ L->elem=(Elemtype *)malloc(sizeof(LIST_INIT_SIZE)); if (!L->elem) exit(OVERFLOW); L->length=0; L->listsize=LIST_INIT_SIZE; return OK;}Status ListInsert(SqList *L,int i,Elemtype e){ Elemtype *newbase,*p,*q; if (i<1||i>L->length+1) return ERROR; if (L->length>=L->listsize) { newbase=(Elemtype *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(Elemtype)); if (!newbase) exit(OVERFLOW); L->elem=newbase; L->listsize+=LISTINCREMENT; } q=&(L->elem[i-1]);//i的前一个位置,使用的是数组 for(p=&(L->elem[L->length-1]);p>=q;--p)//元素右移 *(p+1)=*p; *q=e; ++L->length; return OK;}Status ListDelete(SqList *L,int i,Elemtype &e)//引用{ Elemtype *p,*q; if (i<1||i>L->length) return ERROR; p=&(L->elem[i-1]); e=*p; q=L->elem+L->length-1; printf("p的第一个值%d\n",*p); for (++p;p<=q;++p) { printf("p的值%d\n",*p); *(p-1)=*p; } --L->length; return OK;}//使用指针void MergeList(SqList La,SqList Lb,SqList &Lc){ Elemtype *pc,*pa,*pb,*palast,*pblast; int ppc,ppa,ppb,ppalast,ppblast; pa=La.elem; pb=Lb.elem; ppa=0; ppb=0; ppalast=La.length-1; ppblast=Lb.length-1; Lc.listsize=Lc.length=La.length+Lb.length; pc=Lc.elem=(Elemtype*)malloc(Lc.listsize*sizeof(Lc.listsize*sizeof(Elemtype))); if (!Lc.elem) exit(OVERFLOW); palast=La.elem+La.length-1; pblast=Lb.elem+Lb.length-1; while(pa<=palast&&pb<=pblast) {//归并 printf("%d %d",La.elem[pa],Lb.elem[pb]); if (*pa<=*pb) *pc++=*pa++; else *pc++=*pb++; } while(pa<=palast) *pc++=*pa++; while(pb<=pblast) *pc++=*pb++;}//不用指针,用数组实现/*void MergeList(SqList La,SqList Lb,SqList &Lc){ int ppc=0,ppa,ppb,ppalast,ppblast; ppa=0; ppb=0; ppalast=La.length-1; ppblast=Lb.length-1; Lc.listsize=Lc.length=La.length+Lb.length; Lc.elem=(Elemtype*)malloc(Lc.listsize*sizeof(Lc.listsize*sizeof(Elemtype))); if (!Lc.elem) exit(OVERFLOW); while(ppa<=ppalast&&ppb<=ppblast) { if (La.elem[ppa]>=Lb.elem[ppb]) { Lc.elem[ppc]=Lb.elem[ppb]; ppc++; ppb++; } else { Lc.elem[ppc]=La.elem[ppa]; ppc++; ppa++; } } while(ppa<=ppalast) { Lc.elem[ppc]=La.elem[ppa]; ppc++; ppa++; } while(ppb<=ppblast) { Lc.elem[ppc]=Lb.elem[ppb]; ppc++; ppb++; }}*/void main(){ int elem,e,i=1,j; SqList L; printf("初始化\n"); InitList(&L); printf("L:\n请输入元素:\n"); scanf("%d",&elem); while(elem!=0) { ListInsert(&L,i,elem); printf("请输入元素\n"); scanf("%d",&elem); i++; } printf("输出元素:\n"); for (j=0;j<L.length;j++) { printf("%d ",L.elem[j]); } printf("删除元素:\n"); scanf("%d",&i); ListDelete(&L,i,e); printf("输出元素:\n"); for (j=0;j<L.length;j++) { printf("%d ",L.elem[j]); } SqList L1; InitList(&L1); i=1; printf("L1:\n请输入元素:\n"); scanf("%d",&elem); while(elem!=0) { ListInsert(&L1,i,elem); printf("请输入元素\n"); scanf("%d",&elem); i++; } printf("\nL1输出元素:\n"); for (j=0;j<L1.length;j++) { printf("%d ",L1.elem[j]); } SqList L2; InitList(&L2); MergeList(L,L1,L2); printf("L2输出元素:\n"); for (j=0;j<L2.length;j++) { printf("%d ",L2.elem[j]); } getch();}
- 两个顺序线性表的合并,关于数组指针的操作。
- 数组表示的线性表(顺序表)的基本操作,以及合并两个非递减数组线性表
- 合并两个顺序存储结构的线性表...
- 两个线性表的合并
- 关于动态数组指针操作的两个例子
- 线性表中顺序表的合并
- 数据结构中线性表的基本操作-合并两个线性表-按照元素升序排列
- 数据结构之两个线性表的合并
- 合并两个线性表的实现
- 用Java实现线性表中的顺序存储的学习和源码(合并两个线性表)
- 两个数组的合并
- 两个顺序链表的合并【openjudge】
- 【实验】两个有序顺序表的合并
- 顺序表实现两个集合的合并
- Java如何将两个数组的元素按顺序合并
- 将两个顺序表的合并为顺序表
- 数据结构与算法_合并两个排好顺序的链表或数组
- 顺序表 线性表 数组的区别
- demo(Struts2+Spring+Hibernate)
- IOS开发之----异常处理
- system IPC
- bit-map程序(解决已知某个文件内包含一些电话号码,每个号码为8位数字,统计不同号码的个数)
- Failed to load class org.slf4j.impl.StaticLogger 的解决方案
- 两个顺序线性表的合并,关于数组指针的操作。
- Linux那些事儿 之 ext4文件系统(0) 引子
- USB驱动开发(5)--USB连续读取器
- SQL触发器实例讲解1
- Pro Git学习笔记(一)——起步
- Myeclipse多行注释快捷键及其他
- {"检索 COM 类工厂中 CLSID 为 {10020100-E260-11CF-AE68-00AA004A34D5} 的组件时失败,原因是出现以下错误: 80040154。"} .
- Android之自定义Adapter的ListView
- 创建专有命名空间单实例程序