?? 00.txt
字號:
#include "stdio.h"
#include "malloc.h"
//#include "string.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
struct SqList
{
int *elem;
int length;
int listsize;
};
struct SqList l1;
int InitList_Sq(struct SqList &L) { // 算法2.3
// 構造一個空的線性表L。
L.elem = (int*)malloc(LIST_INIT_SIZE*sizeof(int));
if (!L.elem) return -2; // 存儲分配失敗
L.length=0; // 空表長度為0
L.listsize=LIST_INIT_SIZE;
printf("the empty list is:");// 初始存儲容量
printf("%d",L.listsize);
printf("\n");
return 1;
} // InitList_Sq
int ListInsert_Sq(struct SqList &L, int i, int e) { // 算法2.4
// 在順序線性表L的第i個元素之前插入新的元素e,
// i的合法值為1≤i≤ListLength_Sq(L)+1
int *p;
if (i < 1 || i > L.length+1) return 0; // i值不合法
if (L.length >= L.listsize) { // 當前存儲空間已滿,增加容量
int *newbase = (int *)realloc(L.elem,
(L.listsize+LISTINCREMENT)*sizeof (int));
if (!newbase) return -2; // 存儲分配失敗
L.elem = newbase; // 新基址
L.listsize += LISTINCREMENT; // 增加存儲容量
}
int *q = &(L.elem[i-1]); // q為插入位置
for (p = &(L.elem[L.length-1]); p>=q; --p) *(p+1) = *p;
// 插入位置及之后的元素右移
*q = e; // 插入e
++L.length; // 表長增1
return 1;
} // ListInsert_Sq
void main()
{
int i,e;
InitList_Sq(l1);
for( i=1;i<=6;i++)
{
int j;
scanf("%d",&j);
ListInsert_Sq(l1,i,j); //函數的調用
printf("%3d",l1.elem[i-1]);
}
printf("\n");
ListInsert_Sq(l1,3,3);
for(i=1;i<=l1.length;i++)
printf("%3d",l1.elem[i-1]);
printf("\n");
printf("線性表的插入創建成功!");
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -