头插法输出少一个数,不知道哪里出错了
头插法输出少一个数,不知道是头插法出问题还是输出有问题,还请大佬能够解答#include <stdio.h>
typedef struct abc {
int date;
struct abc *next;
}name;
//创造头接点
name* creathead()
{
name* head=(name*)malloc(sizeof(name));
head->next=NULL;
return head;
}//头插法
void input1(name* head,int n)
{
int i;
for( i=1;i<n;i++){
name* p=(name*)malloc(sizeof(name));
scanf("%d\t",&p->date);
p->next=head->next;
head->next=p;
}
}
//尾插法
void input2(name* head , int n)
{
int i;
name* rear=(name*)malloc(sizeof(name));
rear=head;
for(i=1;i<=n;i++){
name* newnode = (name*)malloc(sizeof(name));
scanf("%d",&newnode->date);
rear->next=newnode;
rear=newnode;
}
rear->next=NULL;
}
//删除相同值
void same(name *head)
{
if(head == NULL)return 0;
name* q=head->next;
name* p=head->next;
while(q->next ){
if(q->date == p ->date)p->next=p->next->next;
p=p->next;
}
q=q->next;
}
//打印
void prin(name* head)
{
name* val=head->next;
while(val)
{
printf("%d\n",val->date);
val=val->next;
}
}//显示
int show()
{
int n;
printf("输入几个数N:");
scanf("%d",&n);
return n;
}
int main(int argc, char *argv[])
{
name* head=creathead();
int n=show();
input1(head,n);
// input2(head,n);
// same(head);
prin(head);
return 0;
} #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct abc
{
int date;
struct abc* next;
}name;
name* createhead()
{
name* head = (name*)malloc(sizeof(name));
head->next = NULL;
return head;
}
void input1(name* head, int n)
{
int i;
for(i=0;i<n;i++)
{
name* p = (name*)malloc(sizeof(name));
scanf("%d", &(p->date));
p->next = head->next;
head->next = p;
}
}
void prin(name* head)
{
name* val = head->next;
while(val)
{
printf("%d\n", val->date);
val = val->next;
}
}
int show()
{
int n;
printf("输入几个数N: ");
scanf("%d", &n);
return n;
}
int main(int argc, char* argv[])
{
name* head = createhead();
int n = show();
input1(head, n);
prin(head);
return 0;
}
input1 函数中的 scanf 多了一个\t, 还有for 循环的判断条件; wangliaodaiqian 发表于 2022-7-18 10:04
input1 函数中的 scanf 多了一个\t, 还有for 循环的判断条件;
谢谢大佬
页:
[1]