输入用例1:
2 4 5 8 10 13 21 0
-1
输出用例1:
插入前的单链表:
2 4 5 8 10 13 21
插入后的单链表:
-1 2 4 5 8 10 13 21
输入用例2:
2 4 5 8 10 13 21 0
7
输出用例2:
插入前的单链表:
2 4 5 8 10 13 21
插入后的单链表:
2 4 5 7 8 10 13 21
输入用例3:
2 4 5 8 10 13 21 0
25
输出用例3:
插入前的单链表:
2 4 5 8 10 13 21
插入后的单链表:
2 4 5 8 10 13 21 25
#include <stdio.h>
#include <stdlib.h>
/**************************************/
/* 链表实现的头文件,文件名slnklist.h */
/**************************************/
typedef int datatype;
typedef struct link_node{
datatype info;
struct link_node *next;
}node;
typedef node *linklist;
/******************************************/
/*函数名称:creatbystack() */
/*函数功能:头插法建立带头结点的单链表 */
/******************************************/
linklist creatbystack()
{
linklist head,s;
datatype x;
head=(linklist)malloc(sizeof(node));
head->next=NULL;
//printf("请输入整数序列(空格分开,以0结束):\n");
scanf("%d",&x);
while(x!=0)
{
s=(linklist)malloc(sizeof(node));
s->info=x;
s->next=head->next;
head->next=s;
scanf("%d",&x);
}
return head;
}
/****************************************/
/*函数名称:creatbyqueue() */
/*函数功能:尾插法建立带头结点的单链表 */
/****************************************/
linklist creatbyqueue()
{
linklist head,r,s;
datatype x;
head=r=(linklist)malloc(sizeof(node));
head->next=NULL;
//printf("请输入整数序列(空格分开,以0结束):\n");
scanf("%d",&x);
while(x!=0)
{
s=(linklist)malloc(sizeof(node));
s->info=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return head;
}
/************************************/
/*函数名称:print() */
/*函数功能:输出带头结点的单链表 */
/************************************/
void print(linklist head)
{
linklist p;
int i=0;
p=head->next;
while(p)
{
printf("%7d",p->info);
i++;
if(i%10==0) printf("\n");
p=p->next;
}
printf("\n");
}
/************************************/
/*函数名称:delList() */
/*函数功能:释放带头结点的单链表 */
/************************************/
void delList(linklist head)
{
linklist p=head;
while(p)
{
head=p->next;
free(p);
p=head;
}
}
linklist insert(linklist head ,datatype x)
{
linklist pre,p,q;
pre=head;
p=head->next;
while(p->info < x && p)
{
pre=p;
p=p->next;
}
q=(linklist)malloc(sizeof(node));
q->info=x;
q->next=pre->next;
pre->next=q;
return head;
}
int main()
{
datatype x;
linklist head;
//printf("输入一组升序排列的整数:\n");
head=creatbyqueue(); /*尾插入法建立带头结点的单链表*/
scanf("%d",&x); /*输入要插入的值*/
printf("插入前的单链表:\n");
print(head); /*输出原链表*/
head=insert(head,x); /*将输入的值插入到带头结点的单链表适当位置*/
printf("插入后的单链表:\n");
print(head); /*输出插入后的链表*/
delList(head);
return 0;
}
用例1和2都可以通过,为什么3通过不了,运行不出来呢