C将元素在链表开头 [英] C Insert Element At Beginning of Linked List

查看:122
本文介绍了C将元素在链表开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被设计为插入以升序结构成一个链表C写的一个程序。

I have written a program in C that is designed to insert structures in an ascending order into a Linked List.

的问题是,是未插入我的两个最低值(1和2)。这是因为我目前没有工作的处理程序,以检查是否链表的第一个值已经大于给定的。

The problem is that is is not inserting my two lowest values (1 and 2). This is because I don't currently have a working handler to check if the first value of the linked list is already greater than the given.

下面是我的功能:

struct PCB
{
    struct PCB *Next_PCB ;
    int PID ;
};

void insert_ordered (struct PCB *Head, struct PCB *Add)
{
    tmp = Head;
    if (Head->PID == 0) {
        Head->PID = Add->PID;
    } else {
        if (Head->Next_PCB == NULL) {
            Head->Next_PCB = Add;
        } else {
            int count = 0;
            while (Head != NULL) {
                if (Add->PID > Head->PID) {
                    if (Head->Next_PCB != NULL) {
                        Head = Head->Next_PCB;
                        count++;
                    } else {
                        Head->Next_PCB = Add;
                        break;
                    }
                } else if (Add->PID == Head->PID) {
                    Add->Next_PCB = Head->Next_PCB;
                    Head->Next_PCB = Add;
                    break;
                } else if (Add->PID < Head->PID) {
                    if (Add->PID == 1 || Add->PID == 2) {
                        printf("found 1 or 2");
                        printf("count: %d", count);
                    }
                    int ct = 0;
                    while (tmp != NULL) {
                        if (count == 0) {
                            printf("made it, %d", ct);
                            Add->Next_PCB = tmp;
                            break;
                        } else if (ct == (count - 1)) {
                            Add->Next_PCB = Head;
                            tmp->Next_PCB = Add;
                            break;
                        }
                        tmp = tmp->Next_PCB;
                        ct++;
                    }
                    break;
                }
            }
        }
    }
    printf("pid : %d\n", Add->PID);
}

下面是打印出清单后,我的输出:

Here is my output after printing out the list:

pid : 6
pid : 17
pid : 15
pid : 13
pid : 15
pid : 6
pid : 12
pid : 9
found 1 or 2count: 0made it, 0pid : 1
found 1 or 2count: 0made it, 0pid : 2
pid : 7
pid : 10
pid : 19

-------------------
PID: 6
PID: 6
PID: 7
PID: 9
PID: 10
PID: 12
PID: 13
PID: 15
PID: 15
PID: 17
PID: 19

的输出应该有两个六点之前a 1和a 2。任何人可以帮助我吗?谢谢你。

The output SHOULD have a 1 and a 2 before the two sixes. Can anybody help me out? Thanks.

推荐答案

无效insert_ordered(结构PCB *头,结构PCB *添加):这是不可能取代在此界面的元件的头部。所以,你需要在虚拟元件的第一个元素(锚节点时无意控股的内容)。结果
下面是一个具体的例子:

void insert_ordered (struct PCB *Head, struct PCB *Add) : It is not possible to replace the head of the elements in this interface. So, you need to the first element in the dummy elements(Anchor node that is not intended contents of the holding).
The following is a specific example:

#include <stdio.h>
#include <stdlib.h>

struct PCB{
    struct PCB *Next_PCB ;
    int PID ;
};

struct PCB *new_PCB(int PID){
    struct PCB *pcb = malloc(sizeof(*pcb));
    if(pcb != NULL){
        pcb->Next_PCB = NULL;
        pcb->PID = PID;
    }
    return pcb;
}

void insert_ordered (struct PCB *Head, struct PCB *Add){
    if(Head == NULL)
        return ;//can't

    struct PCB *prev = Head, *curr = Head->Next_PCB;
/*  this is reduced to following while-loop
    if(Head->Next_PCB == NULL){//empty list
        Head->Next_PCB = Add;
        return ;
    }
    if(Add->PID <= curr-> PID){//Add node <= Top node
        prev->Next_PCB = Add;
        Add->Next_PCB = curr;
        return ;
    }
*/
    while(curr != NULL && curr->PID < Add->PID){
        prev = curr;
        curr = curr->Next_PCB;
    }
    prev->Next_PCB = Add;
    Add->Next_PCB = curr;
}

void print(struct PCB *Head){
    if(Head == NULL)
        return ;
    else
        Head = Head->Next_PCB;
    while(Head != NULL){
        printf("PID: %d\n", Head->PID);
        Head = Head->Next_PCB;
    }
}

//alias
#define Make_list() new_PCB(-1)

int main(void){
    struct PCB *head = Make_list();
    //struct PCB *head = new_PCB(-1);//dummy node, This is intended to holds a list.
    insert_ordered (head, new_PCB( 6));
    insert_ordered (head, new_PCB(17));
    insert_ordered (head, new_PCB(15));
    insert_ordered (head, new_PCB(13));
    insert_ordered (head, new_PCB(15));
    insert_ordered (head, new_PCB( 6));
    insert_ordered (head, new_PCB(12));
    insert_ordered (head, new_PCB( 9));
    insert_ordered (head, new_PCB( 1));
    insert_ordered (head, new_PCB( 2));
    insert_ordered (head, new_PCB( 7));
    insert_ordered (head, new_PCB(10));
    insert_ordered (head, new_PCB(19));

    print(head);
    //release list
    return 0;
}

或者你需要更换像一个代码的内容。

Or you need to swap the contents like a sort.

这篇关于C将元素在链表开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆