线性表的插入问题
以下是插入的程序,想知道为什么while运行两次之后,就不显示Input position,data了。
step了一下,发现到后面while语句里面把printf("Input position,data\n"); 给直接跳过了。
为什么会这样?!
谢谢!
/* Note:Your choice is C IDE */
#include <stdio.h>
#include <stdlib.h>
struct Data{
char *name;
int age;
int Size;
};
int Init_List(struct Data *New_Struct,int Max) /*申请空间*/
{
if((*New_Struct).name=(char *)malloc(sizeof(char *)*Max)==NULL)
{
printf("Init Failed\n");
return -1;
}
}
void Insert_List(struct Data *Des,int Position,char source) /*插入元素到指定位置之后*/
{
int i;
if(Position<=0)
Position=0;
if(Position>Des->Size)
Position=Des->Size;
for(i=(*Des).Size+1;i>Position;i--)
{
*((*Des).name+i)=*((*Des).name+i-1);
}
*((*Des).name+Position)=source;
Des->Size++;
}
main()
{
int d,Max=20;
char s,G='y';
struct Data *p;
Init_List(&p,Max);
p->Size=0; /*初始化*/
p->name="\0";
while(G=='y')
{
printf("Input position,data\n");
scanf("%d %c",&d,&s);
Insert_List(p,d,s);
printf("%s\t%d\n",p->name,p->Size);
printf("Continue?y,n\n");
G=getch();
}
free(p->name);
}