?? 2.21.cpp
字號:
//#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100 //現行表存儲空間的初始分配量
#define LISTINCREMENT 10 //現行表存儲空間的分配量
#define ElemType int
typedef int Status;
typedef struct{
int * elem; //存儲空間基址
int length; //當前長度
int listsize; //當前分配的存儲容量(以sizeof(ElemType)為單位)
}SqList;
Status InitList(SqList *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;
}
Status ReverseList(SqList *L){
int i,t;
(*L).elem[0]=1;
(*L).elem[1]=2;
(*L).elem[2]=3;
(*L).elem[3]=4;
(*L).elem[4]=5;
(*L).elem[5]=6;
(*L).length=6; //初始化線性表各元素
printf("置換前輸出表中數:\n");
for(i=0;i<(*L).length;i++)
if (i<(*L).length-1)
printf("%d->",(*L).elem[i]);
else
printf("%d",(*L).elem[i]); //輸出置換前各元素
for(i=0;i<=(*L).length/2;i++)
{ t=(*L).elem[i];
(*L).elem[i]=(*L).elem[(*L).length-1-i];
(*L).elem[(*L).length-1-i]=t;} //置換元素
printf("\n置換后輸出表中數:\n");
for(i=0;i<(*L).length;i++)
if (i<(*L).length-1)
printf("%d->",(*L).elem[i]);
else
printf("%d",(*L).elem[i]); //輸出置換后各元素
return OK;
}
int main(SqList *p)
{
printf("線性表的逆序-21:\n");
SqList La;
InitList(&La); /* 創建空表La */
ReverseList(&La); /* 置換表中數La */
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -