我用一个C程序创建了一个链表。我的代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
struct node *head = NULL;
int numberOfElements = 0;
//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;
numberOfElements ++;
}
//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;
numberOfElements --;
}
//is list empty
bool isEmpty() {
return head == NULL;
}
int length() {
return numberOfElements;
}
//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) {
numberOfElements --;
//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;
} else {
//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 main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("Original List: ");
//print list
printList();
printf("nlength is %dn",length());
struct node *foundLink = find(4);
if(foundLink != NULL) {
printf("nElement found: ");
printf("(%d,%d) ",foundLink->key,foundLink->data);
printf("n");
} else {
printf("Element not found.");
}
delete(4);
printf("List after deleting an item: ");
printList();
printf("n");
foundLink = find(4);
if(foundLink != NULL) {
printf("nElement found: ");
printf("(%d,%d) ",foundLink->key,foundLink->data);
printf("n");
} else {
printf("Element not found.");
}
printf("nlength is %dn",length());
}
在链表结构中,我只将节点结构定义为结构体,没有将链表结构定义为单独的结构体。通过为链表结构定义一个单独的结构体,如下所示;
struct MyList {
struct node *head;
int numberOfElements;
};
我想从全局可变性中排除变量'head'和'numberOfElements'。有了这个更改,我如何更新上面的代码并运行它?
最简单的解决方案是修改您的列表管理函数,使它们采用struct MyList*
参数,并将对head
和numberOfElements
的所有引用更改为list->head
和list->numberOfElements
,然后将struct MyList
放在任何函数中使用它(在您的情况下为main()
),并将对struct MyList
的引用传递给需要修改它的每个函数,例如:
//find a link with given key
struct node* find(struct MyList *list,int key) {
//start from the first link
struct node* current = list->head;
//if list is empty
if(list->head == NULL) {
return NULL;
...
//is list empty
bool isEmpty(struct MyList *list) {
return list->head == NULL;
}
int length(struct MyList *list) {
return list->numberOfElements;
}
int main(){
struct MyList list = (struct MyList){.head=NULL, .numberOfElements = 0};
insertFirst(&list,1,10);
insertFirst(&list,2,20);
insertFirst(&list,3,30);
insertFirst(&list,4,1);
...