C示例删除循环链接列表中的项目的程序 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; struct node *prev = NULL; //insert link at the first location void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; head->next = link; return; } current = head; // move to the end of the list while(current->next != head) current = current->next; // Insert link at the end of the list current->next = link; // Link the last node back to head link->next = head; } void remove_data(int data) { int pos = 0; if(head==NULL) { printf("Linked List not initialized"); return; } if(head->data == data) { if(head->next != head) { current = head; while(current->next!=head) { current = current->next; } current->next = head->next; head = head->next; return; } else { head = NULL;
C示例程序更新循环链接列表中的项目 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //insert link at the first location void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; head->next = link; return; } current = head; // move to the end of the list while(current->next != head) current = current->next; // Insert link at the end of the list current->next = link; // Link the last node back to head link->next = head; } void update_data(int old, int new) { int pos = 0; if(head==NULL) { printf("Linked List not initialized"); return; } current = head; while(current->next!=head) { if(current->data == old) { current->data = new; printf("\n%d found at position %d, replaced with %d\n", old, pos, new); return; } current = current->next; pos++; } if(curren
C示例在圆形链接列表中查找项目的程序 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //insert link at the first location void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; head->next = link; return; } current = head; // move to the end of the list while(current->next != head) current = current->next; // Insert link at the end of the list current->next = link; // Link the last node back to head link->next = head; } //display the list void find_data(int item) { int pos = 0; if(head == NULL) { printf("Linked List not initialized"); return; } current = head; while(current->next != head) { if(current->data == item) { printf("%d found at position %d\n", item, pos); return; } current = current->next; pos++; } if(current->data == item) { printf("%d
C示例查找循环链接列表大小的程序 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //insert link at the first location void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; head->next = link; return; } current = head; // move to the end of the list while(current->next != head) current = current->next; // Insert link at the end of the list current->next = link; // Link the last node back to head link->next = head; } //display the list void list_size(struct node *list) { int size = 0; if(head==NULL) { printf("List size : %d ", size); return; } current = head; size = 1; while(current->next != head) { current = current->next; size++; } printf("List size : %d ", size); } int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(
C示例查找循环链接列表反向的程序 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //insert link at the first location void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; head->next = link; return; } current = head; // move to the end of the list while(current->next != head) current = current->next; // Insert link at the end of the list current->next = link; // Link the last node back to head link->next = head; } //display the list void reverse_print(struct node *list) { if(list->next == head) { printf(" %d =>",list->data); return; } reverse_print(list->next); printf(" %d =>",list->data); } int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); reverse_print(head); printf(" [head]\n"); return 0; }
C示例程序创建循环链接列表 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //insert link at the first location void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; head->next = link; return; } current = head; // move to the end of the list while(current->next != head) current = current->next; // Insert link at the end of the list current->next = link; // Link the last node back to head link->next = head; } //display the list void printList() { struct node *ptr = head; printf("\n[head] =>"); //start from the beginning while(ptr->next != head) { printf(" %d =>",ptr->data); ptr = ptr->next; } printf(" %d =>",ptr->data); printf(" [head]\n"); } int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); printList(); ret
C示例程序删除单链表中的项目 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; struct node *prev = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; return; } current = head; // move to the end of the list while(current->next!=NULL) current = current->next; // Insert link at the end of the list current->next = link; } void display() { struct node *ptr = head; printf("head] =>"); //start from the beginning while(ptr != NULL) { printf(" %d =>",ptr->data); ptr = ptr->next; } printf(" [null]\n"); } void remove_data(int data) { int pos = 0; if(head==NULL) { printf("Linked List not initialized"); return; } if(head->data == data) { if(head->next != NULL) { head = head->next; return; } else { head = NULL; printf("List i
C示例程序更新单链表中的项目 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; return; } current = head; // move to the end of the list while(current->next!=NULL) current = current->next; // Insert link at the end of the list current->next = link; } void display() { struct node *ptr = head; printf("\n[head] =>"); //start from the beginning while(ptr != NULL) { printf(" %d =>",ptr->data); ptr = ptr->next; } printf(" [null]\n"); } void update_data(int old, int new) { int pos = 0; if(head==NULL) { printf("Linked List not initialized"); return; } current = head; while(current->next!=NULL) { if(current->data == old) { current->data = new; printf("\n%d found at position %d,
C示例在单一链接列表中查找项目的程序 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; return; } current = head; // move to the end of the list while(current->next!=NULL) current = current->next; // Insert link at the end of the list current->next = link; } void find_data(int item) { int pos = 0; if(head==NULL) { printf("Linked List not initialized"); return; } current = head; while(current->next!=NULL) { if(current->data == item) { printf("%d found at position %d\n", item, pos); return; } current = current->next; pos++; } printf("%d does not exist in the list", item); } int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); find_data(40); find