?? 3原.c
字號:
#include <stdio.h>
#define maxsize 10
typedef struct sqlist
{
int data[maxsize];
int last;
} SqlistTp;
int main (void)
{
SqlistTp L;
int input = 0;
int output = 0;
int length = 0;
int count = 1;
Initsqlist(&L);
printf("請輸入一些數字:");
scanf("%d", &input);
while (input != 0)
{
insert_sqlist (&L, input, count);
count++;
scanf("%d", &input);
}
print_sqlist(&L);
length_sqlist(&L, &length);
printf("線性表的順序表長為%d\n", length);
printf("請輸入要插入的數據和相應的位置:");
scanf("%d %d", &input, &count);
insert_sqlist(&L, input, count);
print_sqlist(&L);
length_sqlist(&L, &length);
printf("線性表的順序表長為%d\n", length);
printf("請輸入要讀出的數據的位置:");
scanf("%d", &count);
get_sqlist(&L, count, &output);
printf("第%d個位置的數據是%d\n", count, output);
printf("請輸入要查找(按值查找)的數據:");
scanf("%d", &input);
locate_sqlist(&L, input, &output);
printf("您要查找的數據%d在第%d個位置.\n", input, output);
printf("請輸入要刪除的位置:");
scanf("%d", &count);
delete_sqlist(&L, count);
print_sqlist(&L);
}
int Initsqlist (SqlistTp *L)
{
L -> last = 0;
}
int insert_sqlist (SqlistTp *L, int x, int i)
{
int j;
if (L -> last == maxsize)
{
printf("\n表滿!\n");
return 0;
}
if ((i < 1) || (i > L -> last + 1))
{
printf("\n非法位置!\n");
return 0;
}
for (j = L -> last; j >= i; j--)
L -> data[j] = L -> data[j - 1];
L -> data[i - 1] = x;
L -> last = L -> last + 1;
}
int print_sqlist (SqlistTp *L)
{
int i;
printf("當前線性表的排列為:\n");
for (i = 1; i <= L -> last; i++)
printf("(%d):%d ", i, L -> data[i - 1]);
printf("\n");
}
int length_sqlist (SqlistTp *L, int *length)
{
*length = L -> last;
}
int get_sqlist (SqlistTp *L, int i, int *out)
{
if ((i < 0) || (i > L -> last))
{
printf("\n非法位置!\n");
return 0;
}
else
*out = L -> data[i - 1];
}
int locate_sqlist (SqlistTp *L, int x, int *output)
{
int i = 1;
while ((i <= L -> last) && (L -> data[i - 1] != x))
i++;
if (i <= L -> last)
*output = i;
else
printf("\n查無此數!\n");
}
int delete_sqlist (SqlistTp *L, int i)
{
int j;
if ((i < 1) || (i > L -> last))
{
printf("\n非法位置!\n");
return 0;
}
for (j = i + 1; j <= L -> last; j++)
L -> data[j - 2] = L -> data[j - 1];
L -> last = L -> last -1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -