现在的位置: 首页 > 综合 > 正文

线性表

2018年05月02日 ⁄ 综合 ⁄ 共 766字 ⁄ 字号 评论关闭

      开始复习简单的数据结构啦,O(∩_∩)O哈哈~

      实例一、顺序表的逆置。

#include <stdio.h>

#define MAXSIZE 100
typedef int ElemType;

typedef struct
{
	ElemType data[MAXSIZE];
	int length;
}SqList;

void swap(ElemType *a, ElemType *b)
{
	ElemType t = *a;
	*a = *b;
	*b = t;
}

void ReverseSqList(SqList *L)
{
	int i = 0, j = L->length - 1;
	if(j<=1)
		return;
	while(i<j)
	{
		swap(&L->data[i], &L->data[j]);
		++i;
		--j;
	}
}

void CreateSqList(SqList *L, int sz)
{
	L->length = sz;
	int i = 0;
	printf("input %d data : ", sz);
	while(i<sz)
	{
		scanf("%d", &L->data[i]);
		++i;
	}
}

void PrintSqList(SqList *L)
{
	int i = 0;
	while(i<L->length)
	{
		printf("%d ", L->data[i]);
		++i;
	}
	printf("\n");
}

int main()
{
	SqList L;
	int n;
	printf("input n : ");
	scanf("%d", &n);
	CreateSqList(&L, n);
	ReverseSqList(&L);
	PrintSqList(&L);
	return 0;
}

      话说,那个ReverseSqList函数里面偶写成while(i++<j--)就是不行。等下偶得仔细看看(后来经过测试,原来要把i = i+1, j = j-1算出来之后才会判断 i<j所以,最后一个和第一个没有交换)。

抱歉!评论已关闭.