#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *creat(int a[],int n);
struct node *insert(struct node *head,int x);
void print(struct node *head);
int main()
{
struct node *head=NULL;
int a[10];
int repeat,i,j,n,x;//x-待插入的数据
scanf("%d",&repeat);
for(i=0;i<repeat;i++)
{
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
scanf("%d",&x);
head=creat(a,n);
insert(head,x);
head=insert(head,x);
printf("size=%d:",n+1);
print(head);
printf("\n");
}
return 0;
}
struct node *creat(int a[],int n)
{
int i;
struct node *head=NULL,*end,*p;
end=head;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=a[i];
p->next=NULL;
if(head==NULL)
{
head=p;
end=head;
}
else
{
end->next=p;
end=end->next;
}
}
end->next=NULL;
return head;
}
struct node *insert(struct node *head,int x)
{
struct node *p1=head,*p2=head->next,*p3=NULL,*end=NULL;
p3->data=x;
p3->next=NULL;
for(end=head;end->next!=NULL;end=end->next);//最后一个结点
if(p3->data<head->data)//插入点在头结点前
{
p3->next=head;
return p3;
}
else if(x>end->data)
{
end->next=p3;
return head;
}
else//插入点在链表中间
{
for(p1=head;p1!=NULL,p2!=end;p1=p1->next)
{
p2=p1->next;
if(x>=p1->data&&x<=p2->data)
{
p1->next=p3;
p3->next=p2;
}
}
return head;
}
}
void print(struct node *head)
{
struct node *p;
p=head;
for(p=head;p!=NULL;p=p->next)
{
if(p==head)
printf("%d",p->data);
else
printf(" %d",p->data);
}
}
修改处见注释,供参考:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *creat(int a[],int n);
struct node *insert(struct node *head,int x);
void print(struct node *head);
int main()
{
struct node *head=NULL;
int a[10];
int repeat,i,j,n,x;//x-待插入的数据
scanf("%d",&repeat);
for(i=0;i<repeat;i++)
{
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
head=creat(a,n);
print(head); //修改
printf("\n"); //修改
scanf("%d",&x);
//insert(head,x);//修改
head=insert(head,x);
printf("size=%d:",n+1);
print(head);
printf("\n");
}
system("pause");
return 0;
}
struct node *creat(int a[],int n)
{
int i;
struct node *head=NULL,*end,*p;
end=head;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=a[i];
p->next=NULL;
if(head==NULL)
{
head=p;
end=head;
}
else
{
end->next=p;
end=end->next;
}
}
end->next=NULL;
return head;
}
struct node *insert(struct node *head,int x)
{
struct node *p1=head,*p2=NULL,*p3=NULL; //,*end=NULL;//修改
p3 = (struct node*)malloc(sizeof(struct node)); //修改
p3->data=x;
p3->next=NULL;
//for(end=head;end->next!=NULL;end=end->next);//最后一个结点//修改
while (p1){
if(p3->data < p1->data)//插入点在头结点前 if(p3->data<head->data)//修改
{
if (p1 == head){ //p3->next=head; //修改
p3->next = head;//return p3; //修改
head = p3;
}
else{ //修改
p3->next = p2->next; //修改
p2->next = p3; //修改
}
return head; //修改
}
//else if(x>end->data) //修改
//{
// end->next=p3;
// return head;
//}
else //插入点在链表中间
{
// for(p1=head;p1!=NULL,p2!=end;p1=p1->next) //修改
//{
// p2=p1->next;
//if(x>=p1->data&&x<=p2->data)
// {
// p1->next=p3;
// p3->next=p2;
p2 = p1; //修改
p1 = p1->next;//修改
}
}
if (p1 == NULL) //修改
p2->next = p3;//修改
return head;
}
void print(struct node *head)
{
struct node *p;
p=head;
for(p=head;p!=NULL;p=p->next)
{
if(p==head)
printf("%d",p->data);
else
printf(" %d",p->data);
}
}
打印函数出问题了,p!=NULL,但是你给p的赋值就是head,是空的,所以进去不了循环里面,无法打印。如果还是有不懂的,可以私聊问我.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* creat(int a[], int n);
struct node* insert(struct node* head, int x);
void print(struct node* head);
int main()
{
struct node* head = NULL;
int a[10];
int repeat, i, j, n, x;//x-待插入的数据
scanf("%d", &repeat);
for (i = 0; i < repeat; i++)
{
scanf("%d", &n);
for (j = 0; j < n; j++)
scanf("%d", &a[j]);
scanf("%d", &x);
head = creat(a, n);
insert(head, x);
head = insert(head, x);
printf("size=%d:", n + 1);
print(head);
printf("\n");
}
return 0;
}
struct node* creat(int a[], int n)
{
int i;
struct node* head = NULL, * end, * p;
end = head;
for (i = 0; i < n; i++)
{
p = (struct node*)malloc(sizeof(struct node));
p->data = a[i];
p->next = NULL;
if (head == NULL)
{
head = p;
end = head;
}
else
{
end->next = p;
end = end->next;
}
}
end->next = NULL;
return head;
}
struct node* insert(struct node* head, int x)
{
struct node* p1 = head, * p2 = head->next, * end = NULL;
struct node* p3 = (struct node*)malloc(sizeof(struct node));
p3->data = x;
p3->next = NULL;
for (end = head; end->next != NULL; end = end->next);//最后一个结点
if (p3->data < head->data)//插入点在头结点前
{
p3->next = head;
return p3;
}
else if (x > end->data)
{
end->next = p3;
return head;
}
else//插入点在链表中间
{
for (p1 = head; p1 != NULL, p2 != end; p1 = p1->next)
{
p2 = p1->next;
if (x >= p1->data && x <= p2->data)
{
p1->next = p3;
p3->next = p2;
}
}
return head;
}
}
void print(struct node* head)
{
struct node* p;
p = head->next;
for (p ; p != NULL; p = p->next)
{
if (p == head->next)
printf("%d", p->data);
else
printf(" %d", p->data);
}
}