我现在是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();
但是我仍然不确定如何使其适用于多个链表?有什么想法吗?
答案 0 :(得分:2)
这里的代码使用了全局变量,使得该方法无法再次使用。所以你所能做的就是重写代码,避免使用全局变量。无论这些方法使用什么全局变量,只需将它们相应地传递给方法即可。这样你就可以重复使用它。
答案 1 :(得分:1)
创建包含头部和当前的结构。将其作为参数传递给函数
即
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;
}
我将其他功能作为消费者留给读者