- 论坛徽章:
- 0
|
最近在学数据结构,我不喜欢伪码,希望有具体的实现,但是出了一点小问题
下面是我的代码
- #include <stdio.h>
- #include <malloc.h>
- #define MAXSIZE 1024
- typedef char datatype;
- typedef struct
- {
- datatype data[MAXSIZE];
- int last;
- }sequenlist;
- int insert (sequenlist *L, datatype x, int i)
- {
- int j;
-
- if (L->last == MAXSIZE -1)
- {
- printf("overflow");
- return 0;
- }
- else if ((i < 0) || (i > L->last))
- {
- printf("error,please input the right 'i'");
- return 0;
- }
-
- else
- {
- for (j = L->last; j >= i; j--)
- {
- L->data[j+1] = L->data[j];
- L->data[i] = x;
- L->last = L->last + 1;
-
- }
- }
- return 1;
- }
- int dellist (sequenlist *L, int i)
- {
- if ((i < 0) || (i > L->last))
- {
- printf("error, please input the right i");
- return 0;
- }
-
- else
- {
- for (;i < L->last; i++)
- {
- L->data[i] = L->data[i+1];
- L->last = L->last - 1;
- return 1;
- }
- }
- }
- void creatlist(sequenlist *L)
- {
- int n, i;
- char tmp;
- printf("请输入数据的个数:\n");
- scanf("%d",&n);
-
- for (i = 0; i < n; i++)
- {
- printf("data[%d]=",i);
- fflush(stdin);
- scanf("%c",&tmp);
- L->data[i] = tmp;
- }
- L->last = n-1;
- printf("\n");
- }
- void printout(sequenlist *L)
- {
- int i;
- for (i = 0; i <= L->last; i++)
- {
- printf("data[%d]=",i);
- printf("%c\n", L->data[i]);
- }
- }
- main()
- {
- sequenlist *L;
- char cmd,x;
- int i;
-
- L = (sequenlist*)malloc(sizeof (sequenlist));
- creatlist(L);
- printout(L);
-
- do
- {
- printf("i,I......插入\n");
- printf("d,D......删除\n");
- printf("q,Q......退出\n");
-
- do
- {
- fflush(stdin);
- scanf("%c",&cmd);
- }while((cmd != 'd') && (cmd != 'D') && (cmd != 'q') && (cmd != 'Q') && (cmd != 'i') && (cmd != 'I'));
- switch (cmd)
- {
- case 'i':
- case 'I':
- printf("请输入你要插入的数据:");
- fflush(stdin);
- scanf("%c",&x);
- printf("请输入你要插入的位置:");
- scanf("%d",&i);
- insert(L, x, i);
- printout(L);
- break;
- case 'd':
- case 'D':
- printf("请输入你要删除元素的位置:");
- fflush(stdin);
- scanf("%d",&i);
- dellist(L, i);
- printout(L);
- break;
- }
- }while((cmd != 'q') && (cmd != 'Q'));
- }
复制代码
在Linux,Windows下编译都没有什么问题,但是运行时出了点问题:
为什么“ data[0]=data[1]”连在了一起??
“请输入你要插入的数据:请输入你要插入的位置”这两个地方也连在了一起??
我无法正常输入,
但是在Windows下编译,运行都正常,也是用的gcc编译器
在Linux下运行情况如下:
- ./contiguous-list
- 请输入数据的个数:
- 3
- data[0]=data[1]=5
- data[2]=
- data[0]=
- data[1]=5
- data[2]=
- i,I......插入
- d,D......删除
- q,Q......退出
- i
- 请输入你要插入的数据:请输入你要插入的位置:2
- data[0]=
- data[1]=5
- data[2]=
- data[3]=
- i,I......插入
- d,D......删除
- q,Q......退出
- q
复制代码 |
|