70,023
社区成员




#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20 /*存储空间初始分配量*/
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; /*Status是函数的类型,其值是函数结果状态代码,如OK等*/
typedef int ElemType; /*ElemType类型根据实际情况而定,这里假设为int*/
typedef struct
{
ElemType data[MAXSIZE]; /*数组存储数据元素,最大值为MAXSIZE*/
int length; /*线性表当前长度*/
}SqList;
/*操作结果:构造一个空的线性表*/
Status InitList(SqList *L)
{
L->length = 0;
return OK;
}
/*初始条件:线性表L已存在*/
/*操作结果:销毁线性表L*/
Status DestoryList(SqList *L)
{
if(L)
{
free(L);
L = NULL;
return OK;
}
else
return ERROR;
}
/*初始条件:线性表L已存在*/
/*操作结果:将L重置为空表*/
Status ClearList(SqList *L)
{
L->length = 0;
return OK;
}
/*初始条件:线性表L已存在*/
/*操作结果:若L为空表,则返回TRUE,否则返回FALSE*/
Status ListEmpty(SqList L)
{
if(L.length == 0)
return TRUE;
else
return FALSE;
}
/*初始条件:顺序线性表L已存在,1≤i≤ListLength(L)*/
/*操作结果:用e返回L中第i个数据元素的值*/
Status GetElem(SqList L,int i,ElemType *e)
{
if(L.length == 0 || i < 1 || i > L.length)
return ERROR;
*e = L.data[i-1];
return OK;
}
int main()
{
SqList L;
ElemType e;//元素值
Status s;//返回状态
int i;//元素位序
i = 10;
s = InitList(&L);
s=DestoryList(&L);
//s = GetElem(L,i,&e);
}
SqList L;
s = InitList(&L);
printf("初始化后长度为:%d\n",L.length);
for (i=0;i<MAXSIZE;i++)
{
L.data[i]=i;
}
L.length=i;
for (i=0;i<L.length;i++)
{
printf("%d ",L.data[i]);
}
//带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
int data;
struct NODE *next;
} H,*head,*p,*q,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t,m;
int main() {
srand(time(NULL));
//填写头节点数据
H.data=-1;
H.next=NULL;
head=&H;
//创建10个节点的单链表
p=head;
for (i=0;i<10;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=rand()%100;//填写0..99的随机值
q->next=NULL;
p->next=q;
p=q;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//将值为5的结点插入到单链表的第k个结点前
k=3;
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==n) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=5;
q->next=p->next;
p->next=q;
break;
}
p=p->next;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//删除第k个节点
k=5;
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==n) {
q=p->next;
if (q) {
p->next=q->next;
free(q);
}
break;
}
p=p->next;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//从小到大排序
for (p=head;p!=NULL && p->next!=NULL;p=p->next) {
for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {
if (p->next->data > q->next->data) {
//交换data
// printf("swap %02d %02d\n",p->next->data,q->next->data);
// t=p->next->data;p->next->data=q->next->data;q->next->data=t;
//或者
//交换next
// printf("swap %02d %02d\n",p->next->data,q->next->data);
s1=p->next;
s2=p->next->next;
s3=q->next;
s4=q->next->next;
if (s2!=s3) {
p->next=s3;
s3->next=s2;
q->next=s1;
s1->next=s4;
} else {
p->next=s3;
s3->next=s1;
q=s3;
s1->next=s4;
}
//输出整个单链表
// s=head->next;
// while (1) {
// if (NULL==s) {
// printf("\n");
// break;
// }
// printf("%02d->",s->data);
// s=s->next;
// }
// getchar();
}
}
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10
m=4;
n=6;
k=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
k++;
if (m+1==k) {
q=p;
}
s=p;
p=p->next;
}
s1=head->next;
head->next=q->next;
s->next=s1;
q->next=NULL;
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//释放所有节点
p=head->next;
while (1) {
if (NULL==p) {
break;
}
q=p->next;
free(p);
p=q;
}
return 0;
}
//18->94->58->17->27->20->43->57->75->78->
//18->94->05->58->17->27->20->43->57->75->78->
//18->94->05->58->27->20->43->57->75->78->
//05->18->20->27->43->57->58->75->78->94->
//43->57->58->75->78->94->05->18->20->27->
//
SqList *L = (SqList*)malloc(sizeof(SqList));
s = InitList(L);
printf("初始化后长度为:%d\n",L->length);
for (i=0;i<MAXSIZE;i++)
{
L->data[i]=i;
}
L->length=i;
for (i=0;i<L->length;i++)
{
printf("%d ",L->data[i]);
}
s=DestoryList(L);
在mian()函数中加了个测试,可以运行出正确结果。
SqList *L = (SqList*)malloc(sizeof(SqList));
// ...
s = InitList(L);
s=DestoryList(L);
[/quote]
但这样会导致我的L变成了指针,那换一个思路,如果不应用动态分配(malloc)的话,
按照我的L声明方式,DestoryList该如何正确书写?
SqList *L = (SqList*)malloc(sizeof(SqList));
// ...
s = InitList(L);
s=DestoryList(L);