简体   繁体   English

在c程序中用struct定义链表结构

[英]Defining linked list structure with struct in c program

I created a linked list using a C program.我使用 C 程序创建了一个链表。 My codes are below.我的代码如下。

#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 %d\n",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 %d\n",length());
    
}

In the linked list structure, I only defined the node structure as a struct, the linked list structure is not defined as a separate struct.在链表结构中,我只是将节点结构定义为一个struct,链表结构没有定义为一个单独的struct。 By defining a separate struct for the linked list structure, as follows;通过为链表结构定义一个单独的struct,如下;

struct MyList {
   struct node *head;
   int numberOfElements;
};

I want to exclude the variables 'head' and 'numberOfElements' from global variability.我想从全局可变性中排除变量“head”和“numberOfElements”。 With this change, how can I update my code above and run it?通过此更改,我如何更新上面的代码并运行它?

The simplest solution is to modify your list managing functions so that they take a struct MyList* parameter, and change all references to head and numberOfElements to list->head and list->numberOfElements , then put a struct MyList in whatever function is using it ( main() in your case), and pass a reference to your struct MyList to every function that needs to modify it, for example:最简单的解决方案是修改您的列表管理函数,以便它们采用struct MyList*参数,并将所有对headnumberOfElements的引用更改为list->headlist->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);
   ... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM