这是我的输入和输出,原先输出的是2,插入后输出就变成50了。
这是代码段
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
int date;
struct node*next;
}node,*link;
link fun(link t)
{
node *s;
int c;
int flag=1;
while(flag)
{
c=getchar();
if(c!='*')
{
s=(node*)malloc(sizeof(node));
s->date=c;
s->next=t->next;
t->next=s;
}
else
flag=0;
}
return t;
}
link ins(link t, int i,char e)
{
node *pre,*s;
int k;
pre=t;k=0; /*从头开始查找第i-1个节点*/
while(pre!=NULL&&k<i-1)
{
pre=pre->next;
k=k+1;
}
if(pre==NULL)
{
printf("插入位置不合理!");
}
s=(node*)malloc(sizeof(node));
s->date=e;
s->next=pre->next;
pre->next=s;
printf("success\n");
return t;
}
void main()
{
link t;
t=(link)malloc(sizeof(node));
t->next=NULL;
node *p;
t=fun(t);
p=t->next;
while(p!=NULL)
{
printf("%c\n",p->date);
p=p->next;
}
int e;
printf("请输入插入的字符:");
scanf("%d",&e);
int i;
printf("\n请输入要插入的字符的位置i:");
scanf("%d",&i);
if(0<=i)
{
node *z;
t=ins(t,i,e);
z=t->next;
while(z!=NULL)
{
printf("%d\n",z->date);
z=z->next;
}
}
else
printf("插入位置错误");
}
请老师指点一下。
前面的输出是printf("%c\n",p->date);插入后的输出是printf("%c\n",z->date);
你输入3后按照char类型读入,你之前输入的2按%d输出就是50。
输入插入字符的地方修改一下:
printf("请输入插入的字符:");
scanf("%d",&e);
e = e +48; //这样转换一下
代码修改后如下。如有帮助,请采纳一下,谢谢:
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
int date;
struct node*next;
}node,*link;
link fun(link t)
{
//找到最后一个节点
node* tail = t;
while(tail->next != NULL)
tail = tail->next;
node *s;
int c;
int flag=1;
while(flag)
{
c=getchar();
if(c!='*')
{
s=(node*)malloc(sizeof(node));
s->date=c;
s->next=NULL;
tail->next=s;
tail = s;
}
else
flag=0;
}
return t;
}
link ins(link t, int i,char e)
{
node *pre,*s;
int k;
pre=t;k=0; /*从头开始查找第i-1个节点*/
while(pre!=NULL&&k<i-1)
{
pre=pre->next;
k=k+1;
}
if(pre==NULL)
{
printf("插入位置不合理!");
}
s=(node*)malloc(sizeof(node));
s->date=e;
s->next=pre->next;
pre->next=s;
printf("success\n");
return t;
}
void main()
{
link t;
t=(link)malloc(sizeof(node));
t->next=NULL;
node *p;
t=fun(t);
p=t->next;
while(p!=NULL)
{
printf("%c\n",p->date);
p=p->next;
}
int e;
printf("请输入插入的字符:");
scanf("%d",&e);
e = e +48; //这样转换一下
int i;
printf("\n请输入要插入的字符的位置i:");
scanf("%d",&i);
if(0<=i)
{
node *z;
t=ins(t,i,e);
z=t->next;
while(z!=NULL)
{
printf("%c\n",z->date);
z=z->next;
}
}
else
printf("插入位置错误");
getchar();
getchar();
return;
}
printf("%d\n",z->date);改为printf("%c\n",z->date)就对了
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
char date;
struct node*next;
}node,*link;
link fun(link t)
{
node *s;
char c;
int flag=1;
while(flag)
{
c=getchar();
if(c!='*')
{
s=(node*)malloc(sizeof(node));
s->date=c;
s->next=t->next;
t->next=s;
}
else
flag=0;
}
return t;
}
link ins(link t, int i,char e)
{
node *pre,*s;
int k;
pre=t;k=0; /*从头开始查找第i-1个节点*/
while(pre!=NULL&&k<i-1)
{
pre=pre->next;
k=k+1;
}
if(pre==NULL)
{
printf("插入位置不合理!");
}
s=(node*)malloc(sizeof(node));
s->date=e;
s->next=pre->next;
pre->next=s;
printf("success\n");
return t;
}
void main()
{
link t;
t=(link)malloc(sizeof(node));
t->next=NULL;
node *p;
t=fun(t);
p=t->next;
while(p!=NULL)
{
printf("%c\n",p->date);
p=p->next;
}
char e;
printf("请输入插入的字符:");
e=getchar();
int i;
printf("\n请输入要插入的字符的位置i:");
scanf("%d",&i);
if(i>=0)
{
node *z;
t=ins(t,i,e);
z=t->next;
while(z!=NULL)
{
printf("%c\n",z->date);
z=z->next;
}
}
else
printf("插入位置错误");
}
我把date改成了char型,但是为什么,插入新的字符的时候,没有输入,直接插入的是空格呢?就是在这里没有输入,光标直接跳到输入的位置这块