#define maxSize 100
typdef struct
{
int data[maxSize];//存放顺序表元素的数组
int length;//存放数据表的长度
}Sqlist; //顺序表类型的定义
或者
int A[maxSize];
int n;
typedef struct LNode
{
int data;//存放数据域
struct LNode *next;//指向后继结点的指针
}LNode;//定义单链表结点的类型
typdef struct DLNode
{
int data;
struct DLNode *prior;//前驱
struct DLNode *next;//后继
}DLNode;
LNode *A = (LNode*)malloc(sizeof(LNode))
void findElem (Sqlist L, int e)
{
int i;
for(i = 0; i < L.length; ++i)
if(e == L.data[i])
return i;
return i;
}
int insertElem(Sqlist &L, int p, int e)
{
int i;
if(p < 0 || p > L.length || L.length == maxSize)
return 0;//插入不成功
for(i = L.length - 1; i >= p; --i)
L.data[i+1] = L.data[i];
L.data[p] = e;
++(L.length);
return 1;
}
int deleteElem(Sqlist &L, int p, int &e)
{
int i;
if(p < 0 || p > L.length - 1)
return 0;//位置不对返回0
e = L.data[p];//将删除元素赋值给e
for (i = p; i < L.length - 1; ++i)//前移
L.data[i] = L.data[i+1];
--(L.length);
return 1;
}
void initLisit(SqList &L, int p, int &e)
{
L.length = 0;
}
int getElem(SqList L, int p, int &e)
{
if(p < 0 || p > L.length - 1)
return 0;
e = L.data[p];
return 1;
}
void createlistR(LNode *&C, int a[], int n)
{
LNode *c, *r;//s用来指向新申请的结点,r始终指向C的终端结点
int i;
C = (LNode *)malloc(sizeof(LNode));//申请C的头结点空间
C->next = NULL;
r = C;
for(i = 0; i < n; ++i)//循环申请n个结点来接收数组a中的元素
{
s = (LNode*)malloc(sizeof(LNode));//s指向新申请的结点
s->data = a[i];
r->next = s;//用r来接纳新结点
r = s;//r指向终端结点,以便接纳下一个到来的结点
}
r->next = NULL;
}
void merge(LNode *A, LNode *B, LNode *&C)
{
LNode *p = A->next;
LNode *q = B->next;
LNode *r;
C = A;
C->next = NULL;
free(B);
r = C;
while(p != NULL && q != NULL)
{
if (p->data <= q->data)
{
r->next = p;
p = p->next;
r = r->next;
}
else
{
r->next = q;
q = q->next;
r = r->next;
}
}
r->next = NULL;
if(p != NULL)
r->next = p;
if(q != NULL)
r->next = q;
}
void createlistF(LNode *&C, int a[], int n)
{
LNode *s;
int i;
C = (LNode*)malloc(sizeof(LNode));
C->next = NULL;
for (i = 0; i < n; ++i)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = a[i];
s->next = C->next;//s所指新结点的指针域next指向C中的开始结点
C->next = s;//头结点的指针域next指向s结点, 使得s成为新的开始结点
}
}
void merge(LNode *A, LNode *B, LNode *&C)
{
LNode *p = A->next;
LNode *q = B->next;
LNode *s;
C = A;
C->next = NULL;
free(B);
while(p != NULL&& q != NULL)
{
if (p->data <= q->data)
{
s = q;
q = q->next;
s->next = C->next;
C->next = s;
}
else
{
s = q;
q = q->next;
s->next = C->next;
C->next = s;
}
}
while(p != NULL)
{
s = p;
p = p->next;
s->next = C->next;
C->next = s;
}
while(q != NULL)
{
s = q;
q = q->next;
s->next = C->next;
C->next = s;
}
}
int findAndDelet(LNode *C, int x)
{
LNode *p, *q;
p = C;
while(p->next != NULL)
{
if(p->next->data == x)
break;
p = p->next;
}
if(p->next == NULL)
return 0;
else
{
//删除
q = p->next;
p->next = p->next->next;
free(q);
return 1;
}
}