| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2578 人关注过本帖
标题:在有序链表中插入数据求助
只看楼主 加入收藏
小晓鸭呀
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2020-4-4
结帖率:40%
收藏
已结贴  问题点数:20 回复次数:3 
在有序链表中插入数据求助
在有序链表中插入数据
给定一批严格递增排列的整型数据,给定一个x,若x不存在,则插入x,要求插入后保持有序。存在则无需任何操作。

输入格式:
输入有两行: 第一个数是n值,表示链表中有n个数据。后面有n个数,分别代表n个数据。 第二行是要插入的数。

输出格式:
输出插入后的链表数据,以空格分开。行末不能有多余的空格。

输入样例1:
在这里给出一组输入。例如:

5 1 3 6 9 11
4
输出样例1:
在这里给出相应的输出。例如:

1 3 4 6 9 11


**********为啥有错,是空串的话要怎么写,谢谢了,急需*************
#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

void InsertList(struct node *head, int n, int x)
{
    struct node *p;
    p = head;
    if (p->next == NULL)
    {
        struct node *q = (struct node *)malloc(sizeof(struct node));
        head = q;
        q->data = x;
        /*q->next = NULL;
        p->next = q;
        p = q;*/

        //q->data = x;
        q->next = NULL;
        return;
    }
    while (p->next != NULL)
    {
        struct node *temp = p;
        p = p->next;
        if (p->data == x)
        {
            return;
        }
        if (p->data > x)
        {
            struct node *q = (struct node *)malloc(sizeof(struct node));
            q->data = x;
            q->next = p;
            temp->next = q;
            return;
        }
    }

    struct node *q = (struct node *)malloc(sizeof(struct node));
    q->data = x;
    q->next = NULL;
    p->next = q;
    p = q;
    return;
}

int main()
{
    struct node *head,*p,*q,*t;
    int i, n, a;
    scanf("%d", &n);
    head = NULL;   //头指针为空
    for (i = 0; i < n; i++){//读入n个数
        scanf("%d", &a);
        //动态申请一个空间,用来存放一个结点,并用临时指针p指向这个结点
        p = (struct node *)malloc(sizeof(struct node));
        p -> data = a;//将数据存储到当前结点的data域中
        p -> next = NULL;//设置当前结点的后继指针指向为空,也就是当前结点的下一个结点为空
        if (head == NULL){
            head = p;//如果这是第一个结点,则将头指针指向这个结点
        }
        else {
            q->next = p;//如果不是第一个创建的结点,则将上一个结点的后继指针指向当前结点
        }
        q = p;//指针q也指向当前结点
    }
    scanf("%d", &a);
    InsertList(head, n, a);
    t = head;
    while (t != NULL){
        printf("%d", t->data);
        if(t->next != NULL)
        {
            printf(" ");
        }
        t = t->next;
    }
    return 0;
}
搜索更多相关主题的帖子: 结点 NULL struct next node 
2020-06-04 13:03
小晓鸭呀
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2020-4-4
收藏
得分:0 
****这咋改成C语言哇********

#include <iostream>
using namespace std;

typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode* next;
} LNode, * LinkList;


void CreateList(LinkList& L, int n)
{
    L = new LNode;
    L->next = NULL;
    LNode* p = L;
    int m;
    for (int i = 0; i < n; i++)
    {
        cin >> m;
        p->next = new LNode;
        p = p->next;
        p->data = m;
        p->next = NULL;
    }
}

void InsertList(LinkList& L, int n, int x)
{
    LNode* p = L;
    if (p->next == NULL)//这种情况下是为空链表直接插入节点的
    {
        LNode* q = new LNode;
        q->data = x;
        q->next = NULL;
        p->next = q;
        p = q;
        return;
    }
    while (p->next != NULL)
    {
        LNode* temp = p;
        p = p->next;
        if (p->data == x) return;
        if (p->data > x)
        {
            LNode* q = new LNode;
            q->data = x;
            q->next = p;
            temp->next = q;
            return;
        }
    }

    LNode* q = new LNode;
    q->data = x;
    q->next = NULL;
    p->next = q;
    p = q;
    return;

}

void OutputList(LinkList L)
{
    LNode* p = L->next;
    while (p != NULL) {
        cout << p->data;
        if (p->next != NULL)
            cout << " ";
        p = p->next;
    }
}

int main()
{
    int n, x;
    cin >> n;
    LinkList L;
    CreateList(L, n);

    cin >> x;
    InsertList(L, n, x);
    OutputList(L);
    return 0;
}
2020-06-04 15:11
ditg
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:16
帖 子:852
专家分:1937
注 册:2014-4-10
收藏
得分:7 
作业?

梦想拥有一台龙芯3A-4000
2020-06-04 15:28
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10538
专家分:42927
注 册:2014-5-20
收藏
得分:7 
#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};

struct node* addData(int x)
{
    struct node* p = (struct node *)malloc(sizeof(struct node));
    if (p == NULL)
        return NULL;
    p->data = x;
    p->next = NULL;
    return p;
}

struct node* InsertList(struct node *head, int x)
{
    if (!head)
        return addData(x);
    if (head->data > x)
    {
        struct node* p = addData(x);
        if (!p)
            return head;
        p->next = head;
        return p;
    }
    struct node *q, *t=NULL;
    for (q=head; q && q->data<x; q=q->next)
        t = q;
    if (!q || q->data>x)
    {
        struct node* p = addData(x);
        if (p)
        {
            t->next = p;
            p->next = q;
        }
    }
    return head;
}

void PrintList(struct node *head)
{
    for (; head; head=head->next)
        printf("%d ", head->data);
    printf("\n");
}

int main()
{
    struct node *head=NULL;
    int i, n, a;
    printf("输入总个数:");
    scanf("%d", &n);
    for (i = 0; i < n; i++) //读入n个数
    {
        scanf("%d", &a);
        head = InsertList(head, a);
    }
    PrintList(head);
    return 0;
}
2020-06-04 16:14
快速回复:在有序链表中插入数据求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037666 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved