`

顺序表的就地逆置

阅读更多

 

源代码:

 

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int Status;   
typedef int ElemType;
#define LIST_INIT_SIZE 100   //初始容量 
#define LISTINCREMENT 10    //空间增量 

typedef struct{
   ElemType *elem;   //存储空间基址
   int length;    //表长,元素个数
   int listsize;   //表容量,空间大小
}SqList; 

Status InitList_Sq(SqList &L)  {
	//构造一个空的线性表L

	L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)) ; 
	if(!L.elem) exit(OVERFLOW); 
	L.length=0;            
	L.listsize=LIST_INIT_SIZE;  
	return OK; 
} 

void Input_Sq(SqList &L) {
	//输入数据
	int n,i,x;
	printf("\nPlease input the SqList's length:");
	scanf("%d",&n);
	L.length=n;
	for(i=0;i<n;i++)
	{
		printf("Please input %dth elem:",i+1);
		scanf("%d",&x);
		L.elem[i]=x;
	}
}


void Print_Sq(SqList L){
	int i,n=L.length;
	printf("The list elemt is:\n");
	for(i=0;i<n;i++)
		printf("%d",L.elem[i]);
}

void Reverse_Sq(SqList &L){
	//利用原表空间就地逆置顺序表L
	int i,n;
	ElemType temp;
	n=L.length;
	for(i=0;i<n/2;i++){
		temp=L.elem[n-1-i];
		L.elem[n-1-i]=L.elem[i];
		L.elem[i]=temp;
	}
}

void main(){
	SqList L;
	ElemType e;
	InitList_Sq(L);
	Input_Sq(L);
	printf("\nBefore Reverse,");
	Print_Sq(L);
	printf("\n");
	Reverse_Sq( L);
	printf("\nAfter Reverse,");
	Print_Sq(L);
	printf("\n");
}

 

 

 

运行结果: 

 



 

 

  • 大小: 5.5 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics