线性表的逆置

写的比较简单,语法上的细节没有做过多的处理,一般数据是可以处理的,之后会多考虑细节的。

问题:

设有一线性表e=(e1 , e2 , … , en-1 , en),其逆线性表定义为e’=( en , en-1 , … , e2 , e1),请设计一个算法,将线性表逆置,要求逆线性表仍占用原线性表的空间,并且用顺序表和单链表两种方法来表示,写出不同的处理函数。

顺序表

#include "stdio.h"
#include "stdlib.h"
int main()
{
	int i,n,rer;
	int *L;
	scanf("%d",&n);
	L=(int *)malloc(sizeof(int)*n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&L[i]);
	}
	for(i=0;i<=(n/2);i++)
	{
		rer=L[i];
		L[i]=L[n-i-1];
		L[n-i-1]=rer;
	}
	for(i=0;i<n;i++)
	{
		printf("%5d",L[i]);
	}
	return 0;
}

双向线性表

#include "stdio.h"
#include "stdlib.h"
struct student
{
	int data;
	struct student *prer,*next;
};
struct student *CreateList();
void nizhi(struct student *L);
int main()
{
	struct student *L;
	L=CreateList();
	nizhi(L);

}

struct student *CreateList()
{
	int x;
	struct student *pHead=NULL;
	struct student *pEnd,*pNew;
	pHead=(struct student *)malloc(sizeof(struct student));
	pHead->next=NULL;
	pHead->prer=NULL;
	pEnd=pHead;
    while(x!=-1)
	{
        pNew=(struct student*)malloc(sizeof(struct student));
		scanf("%d",&x);
		if(x!=-1)
		{
			pNew->data=x;
			pEnd->next=pNew;
			pNew->prer=pEnd;
			pNew->next=NULL;
			pEnd=pNew;
		}
		else continue;
    }
    pEnd->next=pHead;
    pHead->prer=pEnd;
	return pHead;
}

void nizhi(struct student *L)
{
	struct student *pHead;
	pHead=L;
	while(pHead->prer!=L)
	{
		printf("%5d",pHead->prer->data);
		pHead=pHead->prer;
	}
}

代码也没怎么注释,可能会不好理解,有时间我不断完善一哈。

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值