...输出该单链表,然后插入元素X保持单链表的升序排列,输..._百度...
发布网友
发布时间:2024-10-03 11:45
我来回答
共5个回答
热心网友
时间:2024-10-14 14:24
楼主你好,我写了一个程序,应该能满足你的上述要求,希望能帮到你的忙~!
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void insert(struct Node *L,int x)//函数的功能是将x插入到链表L中的合适位置,使链表保持升序
{
struct Node *p = L->next,*p1 = L,*q;//p指向链表的第一个非头结点,p1指向其之前的结点
q = (struct Node *)malloc(sizeof(struct Node));//q指向一个新分配的结点
if (L->next == NULL) //链表为空,则将x作为第一个元素直接插入链表
{
L->next = q;
q->data = x;
q->next = NULL;
return ;
}
while(p && (p->data < x))//否则寻找适合x的插入位置
{
p1 = p;//p1保存的是p之前的结点
p = p->next;
}//循环结束后p指向一个空位置或首个大于x的结点
if (p)//若p没指向一个空位置
{
q->data = x;
p1->next = q;
q->next = p;
}
else
{
q->data = x;
p1->next = q;
q->next = NULL;
}
}
int main()
{
int num;
struct Node *L,*p;
L = (struct Node *)malloc(sizeof(struct Node));
L->next = NULL;
printf("开始创建单链表,请输入链表元素(正整数),输入-1代表结束:\n");
while(scanf("%d",&num) && (num != -1))
{
insert(L,num);
}
p = L->next;
printf("链表中的元素已排列插入完毕.所有的元素为\n");
while(p!=NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return 0;
}
热心网友
时间:2024-10-14 14:23
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
热心网友
时间:2024-10-14 14:18
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
int elem;
struct list * next;
}L;
int main() {
L * l, * p, * q;int a, X;
l = ( L * ) malloc ( sizeof( L ) );L->next = NULL;
/* 输入十个元素 */
for (i = 0;i < 10;i++) {
scanf("%d", &a);
for (p = l;p->next != NULL;p = p->next) {
if (p->next->elem <= a) {break;}
}
q = ( L * ) malloc ( sizeof( L ) );q->elem = a;
q->next = p->next;p->next = q;
}
for (p = l;p->next != NULL;p = p->next) {
printf("%d\n", p->next->elem);
}
/* 输入要插入的元素X */
scanf("%d", &X);
for (p = l;p->next != NULL;p = p->next) {
if (p-.next->elem <= X) {break;}
}
q = ( L * ) malloc ( sizeof( L ) );q->elem = X;
q->next = p->next;p->next = q;
for (p = l;p->next != NULL;p = p->next) {
printf("%d\n", p->next->elem);
}
return 0;
}
热心网友
时间:2024-10-14 14:23
#include <stdio.h>
#include <stdlib.h>
typedef struct db_node {
int data;
struct db_node *next;
}node,*pnode;
pnode init() //创建链表头
{
pnode head = NULL;
head = (pnode)malloc(sizeof(node));
if(head == NULL)
{
printf("memeroy error!\n");
exit(0);
}
head->data = 0;
head->next = NULL;
return head;
}
pnode swap_insert(pnode p,pnode s) //排序插入
{
pnode q;
pnode swap = p;
if(p->next == NULL) //只有一个元素时,比较一次
{
if(s->data < p->data)
{
s->next = p;
p = s;
}else {
p->next = s;
s->next = NULL;
}
return p;
}
q = p->next;
if(s->data <= p->data) //如果小于第一个元素则插在开头
{
s->next = p;
p = s;
}else {
while(s->data > q->data && q->next != NULL) //遍历链表,一直到有比s大的节点或链表尾结束
{
p = p->next;
q = p->next;
}
if(s->data >= q->data && q->next == NULL) //判断是什么原因退出while循环
{
s->next = q->next;
q->next = s;
}else {
p->next = s;
s->next = q;
}
p = swap;
}
return p;
}
pnode create(int number) //初始化链表
{
pnode head,s,p,swap;
int i,value;
head = init();
printf("Enter %d number\n",number);
for(i=0;i<number;i++)
{
s = (pnode)malloc(sizeof(node));
scanf("%d",&value); //接收每个元素
if(s == NULL)
{
printf("error\n");
return ;
}
s->data = value;
if(head->next == NULL)
{
head->next = s;
s->next = NULL;
p = s;
}else
{
p = swap_insert(p,s); //排序插入
}
}
head->next = p;
return head;
}
void dispaly(pnode head) //遍历显示
{
pnode p;
if(head->next == NULL)
{
printf("empty\n");
return;
}
p = head->next;
while(p->next != NULL)
{
printf("%3d",p->data);
p = p->next;
}
printf("%3d\n",p->data);
}
pnode insert(pnode head,int value)
{
pnode p,s;
p = head->next;
s = (pnode)malloc(sizeof(node));
if(s == NULL)
{
printf("error\n");
return ;
}
s->data = value;
s->next = NULL;
p = swap_insert(p,s); //排序插入
head->next = p;
return head;
}
int main()
{
pnode head;
int n,value;
printf("enter the number n\n"); //输入初始化元素个数
scanf("%d",&n);
head = create(n); //初始化一个链表
dispaly(head); //显示链表
while(1) {
printf("enter the insert value or 0 for quit!\n"); //输入要插入的元素或者0退出程序
scanf("%d",&value);
if(value == 0)
{
return 0;
}
head = insert(head,value);
dispaly(head);
}
return 0;
}
热心网友
时间:2024-10-14 14:16
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int a;
struct node* next;
}node;
int main()
{
printf("enter the number of elements\n");
int n;
scanf("%d",&n);
printf("enter the ascending elements \n");
node *head,*p,*q;
if(n==0)
head=NULL;
else
{
head=(node *)malloc(sizeof(node));
scanf("%d",&head->a);
--n;
q=head;
while(n--)
{
p=(node *)malloc(sizeof(node));
q->next=p;
scanf("%d",&p->a);
q=p;
}
q->next=NULL;
}
printf("the list is:\n");
q=head;
while(q!=NULL)
{
printf("%d ",q->a);
q=q->next;
}
printf("\ninsert the elemnt\n");
int m;
scanf("%d",&m);
if(head==NULL)
{
head=(node *)malloc(sizeof(node));
head->a=m;
head->next=NULL;
}
else if(m<(head->a))
{
p=(node *)malloc(sizeof(node));
p->a=m;
p->next=head;
head=p;
}
else
{
q=head;
while(q!=NULL)
{
if(q->next==NULL)
{
p=(node *)malloc(sizeof(node));
p->a=m;
p->next=NULL;
q->next=p;
}
else if(m<(q->next->a))
{
p=(node *)malloc(sizeof(node));
p->a=m;
p->next=q->next;
q->next=p;
break;
}
q=q->next;
}
}
printf("now the list is:\n");
q=head;
while(q!=NULL)
{
printf("%d ",q->a);
q=q->next;
}
return 0;
}