seqlist
#include <stdio.h>
#define MAXLEN 100
#define BOOL int
#define TRUE 1
#define FALSE 0
typedef int elementType;
struct sList
{
elementType data[MAXLEN];
int listLen;
};
typedef struct sList seqList;
int getElement(seqList L,int i,elementType *x);
void ListMerge(seqList A,seqList B,seqList *pc);
int listInsert(seqList *L,elementType x,int i);
int listLocate(seqList L,elementType x);
int insert(seqList *L,elementType x);
BOOL subset(seqList A,seqList B);
BOOL SUBSET(seqList *A,seqList *B);//ordered set
void merge(seqList *A,seqList *B,seqList *C);//merge two ordered set
int main(int argc, char const *argv[])
{
//printf("hello\n");
seqList A={
{1,4,5,9,11},
5
};
seqList B={
{1,4,7,9,11},
5
};
seqList C={//bound to be initialized
{},
0
};
merge(&A,&B,&C);
printf("%d\n",C.listLen );
}
int getElement(seqList L,int i,elementType *x)
{
if(i<1||i>L.listLen)
return 0;
else
{
(*x)=L.data[i-1];
return 1;
}
}
void ListMerge(seqList A,seqList B,seqList *pc)
{
int i=0;
elementType x='l';
for(i=0;i<A.listLen;i++)
{
getElement(A,i+1,&x);
listInsert(pc,x,pc->listLen+1);
}
for(i=0;i<B.listLen;i++)
{
getElement(B,i+1,&x);
if(!listLocate(A,x))
listInsert(pc,x,pc->listLen+1);
}
}
int listInsert(seqList *L,elementType x,int i)
{
int j;
if(L->listLen==MAXLEN)
return 0;
else if(i<1||i>L->listLen+1)
return 1;
else
{
for(j=L->listLen-1;j>=i-1;j
L->data[j+1]=L->data[j];
L->data[i-1]=x;
L->listLen++;
return 2;
}
}
int listLocate(seqList L,elementType x)
{
int i;
for(i=0;i<L.listLen;i++)
if(L.data[i]==x)
return i+1;
return 0;
}
int insert(seqList *L,elementType x)
{
int i=L->listLen-1;
if(i>=MAXLEN-1)
return 0;
else
{
while(i>=0&&L->data[i]>x)
{
L->data[i+1]=L->data[i];
i
}
L->data[i+1]=x;
L->listLen++;
}
}
BOOL subset(seqList A,seqList B)
{
int ia,ib;
elementType x;
int suc;
for(ia=0;ia< A.listLen;ia++)
{
x=A.data[ia];
ib=0;
suc=0;
while(ib<B.listLen && suc==0){
if(x==B.data[ib])
suc=1;
else ib++;
}
if(suc==0) return FALSE;
}
return TRUE;
}
BOOL SUBSET(seqList *A,seqList *B)
{
int ia=0,ib=0;
while(ia<A->listLen&& ib<B->listLen)
{
if(A->data[ia]==B->data[ib])
{ia++;ib++;}
else if(A->data[ia]>B->data[ib]) ib++;
else return FALSE;
}
if(ia>=A->listLen) return TRUE;
else return FALSE;
}
void merge(seqList *A,seqList *B,seqList *C)
{
int ia=0,ib=0,ic=0;
while(ia<A->listLen && ib<B->listLen)
{
if(A->data[ia]==B->data[ib])
{
C->data[ic++]=A->data[ia++];
C->data[ic++]=B->data[ib++];
}
else if (A->data[ia]>B->data[ib]) C->data[ic++]=B->data[ib++];
else C->data[ic++]=A->data[ia++];
while(ia<A->listLen) C->data[ic++]=A->data[ia++];
while(ib<B->listLen) C->data[ic++]=B->data[ib++];
C->listLen=ic;
}
}
链表
typedef int elementType;
struct slNode
{
elementType data;
struct slNode *next;//point to slnode
};
typedef struct slNode node,*linkList;
void initialList(node **pL);
int listLength(node *L);
node *getElement(node *L,int i);
node *listLocate(node *L,elementType x);
bool listInsert(node *L,int i,elementType x);
bool listDelete(node *L,int i);
void createList(node **L);
int main(int argc, char const *argv[])
{
int x;
scanf("%d",&x);
}
void initialList(node **pL)
{
(*pL)=(node *)malloc(sizeof(node));
(*pL)->next=NULL;
}
int listLength(node *L)
{
int len=0;node *p=L->next;
while(p!=NULL)
{
len++;
p=p->next;
}
return len;
}
node *getElement(node *L,int i)
{
node *p=L->next;int j=1;
while(j!=i && p!=NULL)
{
j++;
p=p->next;
}
return p;
}
node *listLocate(node *L,elementType x)
{
node *p=L->next;
while(p!=NULL&&p->data!=x)
p=p->next;
return p;
}
bool listInsert(node *L,int i,elementType x)
{
node *p=L;
node *S;
int k=0;
while(k!=i-1&&p!=NULL)
{
p=p->next;
k++;
}
if(p==NULL)
return false;
else
{
S=(node *)malloc(sizeof(node));
S->data=x;
S->next=p->next;
p->next=S;
return true;
}
}
bool listDelete(node *L,int i)
{
node *u;
node *p=L;
int k=0;
while(k!=i-1&&p!=NULL)
{
p=p->next;
k++;
}
if(p==NULL||p->next==NULL)
return false;
else
{
u=p->next;
p->next=u->next;
free(u);
return true;
}
}
void createList(node **L)
{
elementType x;
node *u, *R;
*L=(node*)malloc(sizeof(node));
(*L)->next=NULL;
R=*L;
printf("please input number(9999 out)\n");
scanf("%d",&x);
while(x!=9999)
{
u=(node*)malloc(sizeof(node));
u->data=x;
u->next=NULL;
R->next=u;
R=u;
scanf("%d",&x);
}
}