C语言数据结构双链表

#include stdio.h#include string.h#include stdlib.h#include stdbool.hstruct node {

编程学习网为您整理以下代码实例,主要实现:C语言数据结构双链表,希望可以帮到各位朋友。

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

struct node {
   int data;
   int key;

   struct node *next;
   struct node *prev;
};

//this link always point to first link
struct node *head = NulL;

//this link always point to last link 
struct node *last = NulL;

struct node *current = NulL;

//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;
}

//display the List in from first to last
voID displayForward() {

   //start from the beginning
   struct node *ptr = head;

   //navigate till the end of the List
   printf("\n[ ");

   while(ptr != NulL) {        
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }

   printf(" ]");
}

//display the List from last to first
voID displayBackward() {

   //start from the last
   struct node *ptr = last;

   //navigate till the start of the List
   printf("\n[ ");

   while(ptr != NulL) {    

      //print data
      printf("(%d,%d) ",ptr->key,ptr->data);

      //move to next item
      ptr = ptr ->prev;

   }

}

//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;

   if(isEmpty()) {
      //make it the last link
      last = link;
   } else {
      //update first prev link
      head->prev = link;
   }

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

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

//insert link at the last location
voID insertLast(int key, int data) {

   //create a link
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;

   if(isEmpty()) {
      //make it the last link
      last = link;
   } else {
      //make link a new last link
      last->next = link;     

      //mark old last node as prev of new link
      link->prev = last;
   }

   //point last to new last node
   last = link;
}

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

   //save reference to first link
   struct node *templink = head;

   //if only one link
   if(head->next == NulL){
      last = NulL;
   } else {
      head->next->prev = NulL;
   }

   head = head->next;
   //return the deleted link
   return templink;
}

//delete link at the last location

struct node* deleteLast() {
   //save reference to last link
   struct node *templink = last;

   //if only one link
   if(head->next == NulL) {
      head = NulL;
   } else {
      last->prev->next = NulL;
   }

   last = last->prev;

   //return the deleted link
   return templink;
}

//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;
      } 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
      current->prev->next = current->next;
   }    

   if(current == last) {
      //change last to point to prev link
      last = current->prev;
   } else {
      current->next->prev = current->prev;
   }

   return current;
}

bool insertAfter(int key, int newKey, int data) {
   //start from the first link
   struct node *current = head; 

   //if List is empty
   if(head == NulL) {
      return false;
   }

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

      //if it is last node
      if(current->next == NulL) {
         return false;
      } else {           
         //move to next link
         current = current->next;
      }
   }

   //create a link
   struct node *newlink = (struct node*) malloc(sizeof(struct node));
   newlink->key = newKey;
   newlink->data = data;

   if(current == last) {
      newlink->next = NulL; 
      last = newlink; 
   } else {
      newlink->next = current->next;         
      current->next->prev = newlink;
   }

   newlink->prev = current; 
   current->next = newlink; 
   return true; 
}

voID main() {
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56); 

   printf("\nList (First to Last): ");  
   displayForward();

   printf("\n");
   printf("\nList (Last to first): "); 
   displayBackward();

   printf("\nList , after deleting first record: ");
   deleteFirst();        
   displayForward();

   printf("\nList , after deleting last record: ");  
   deleteLast();
   displayForward();

   printf("\nList , insert after key(4) : ");  
   insertAfter(4,7, 13);
   displayForward();

   printf("\nList  , after delete key(4) : ");  
   delete(4);
   displayForward();
}
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐