写的比较简单,语法上的细节没有做过多的处理,一般数据是可以处理的,之后会多考虑细节的。
问题:
设有一线性表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;
}
}