Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        2年前关闭。
                                                                                            
                
        
我是C语言的新手,对我来说,这对我来说是种火箭科学,所以我想更好地理解它。

我有以下链接列表实现:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "linked_list.h"

struct Node *head = NULL;
struct Node *current = NULL;

//display the list
void printList() {
    struct Node *ptr = head;
    printf("\n[ ");

    //start from the beginning
    while (ptr != NULL) {
        printf("(%d,%d) ", ptr->key, ptr->data);
        ptr = ptr->next;
    }

    printf(" ]");
}

//insert link at the first location
void insertFirst(int key, int data) {
    //create a link
    struct Node *link = (struct Node*) malloc(sizeof(struct Node));

    link->key = key;
    link->data = data;

    //point it to old first Node
    link->next = head;

    //point first to new first Node
    head = link;
}

//delete first item
struct Node* deleteFirst() {

    //save reference to first link
    struct Node *tempLink = head;

    //mark next to first link as first
    head = head->next;

    //return the deleted link
    return tempLink;
}

//is list empty
bool isEmpty() {
    return head == NULL;
}

int length() {
    int length = 0;
    struct Node *current;

    for (current = head; current != NULL; current = current->next) {
        length++;
    }

    return length;
}

//find a link with given key
struct Node* find(int key) {
    //start from the first link
    struct Node* current = head;

    //if list is empty
    if (head == NULL) {
        return NULL;
    }

    //navigate through list
    while (current->key != key) {

        //if it is last Node
        if (current->next == NULL) {
            return NULL;
        }
        else {
            //go to next link
            current = current->next;
        }
    }

    //if data found, return the current Link
    return current;
}

//delete a link with given key
struct Node* delete(int key) {
    //start from the first link
    struct Node* current = head;
    struct Node* previous = NULL;

    //if list is empty
    if (head == NULL) {
        return NULL;
    }

    //navigate through list
    while (current->key != key) {

        //if it is last Node
        if (current->next == NULL) {
            return NULL;
        }

        //store reference to current link
        previous = current;
        //move to next link
        current = current->next;
    }

    //found a match, update the link
    if (current == head) {
        //change first to point to next link
        head = head->next;
    }
    else {
        //bypass the current link
        previous->next = current->next;
    }

    return current;
}

void sort() {
    int size = length();
    int k = size;

    for (int i = 0; i < size - 1; i++, k--) {
        struct Node *current = head;
        struct Node *next = head->next;

        for (int j = 1; j < k; j++) {
            if (current->data > next->data) {
                int temp_data = current->data;
                current->data = next->data;
                next->data = temp_data;

                int temp_key = current->key;
                current->key = next->key;
                next->key = temp_key;
            }

            current = current->next;
            next = next->next;
        }
    }
}


但是我意识到,我只能用此代码在自己的主要功能中创建一个链表。

我需要多个链接列表(不同类型的链接列表,例如在Java中,例如new LinkedList<T>),因此我创建了一个供不同类使用的标头。

#pragma once
typedef struct Node {
    int data;
    int key;
    struct Node *next;
} Node;

void printList();
void insertFirst(int key, int data);
struct Node* deleteFirst();
bool isEmpty();
struct Node* find(int key);
struct Node* delete(int key);
void sort();


但是我仍然不确定如何使它在多个链接列表中正常工作?有任何想法吗?

最佳答案

创建一个包含水头和水流的结构。并将其作为参数传递给函数



typedef struct {
   struct Node *head;
   struct Node *current;
} MyList;

MyList *CreateMyList() {
   MyList * list= malloc(sizeof(MyList));
   list->current = list->head = NULL;
   return list;
}


void insertFirst(MyList *list, int key, int data) {
    //create a link
    struct Node *link = malloc(sizeof(struct Node));

   link->key = key;
   link->data = data;

   //point it to old first Node
   link->next = list->head;
   //point first to new first Node
   list->head = link;
}


我将其他功能留给读者

关于c - 多个链接列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46999382/

10-12 20:19