编程札记

C

C示例程序拆分循环链接列表

C示例程序拆分循环链接列表 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *even = NULL; struct node *odd = NULL; struct node *list = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); struct node *current; link->data = data; link->next = NULL; if(list == NULL) { list = link; list->next = link; return; } current = list; while(current->next != list) current = current->next; // Insert link at the end of the list current->next = link; link->next = list; } void display(struct node *head) { struct node *ptr = head; printf("[head] =>"); //start from the beginning while(ptr->next != head) { printf(" %d =>",ptr->data); ptr = ptr->next; } printf(" %d =>",ptr->data); printf(" [head]\n"); } void split_list() { int count = 0; // Allocate memory for new node; struct node *list1; struct node *link; struct node *current; list1 = list; while(list1->next != list) { struct

阅读全文 ↗
C

C示例删除循环链接列表中的项目的程序

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

C示例程序更新循环链接列表中的项目

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

C示例在圆形链接列表中查找项目的程序

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

C示例查找循环链接列表大小的程序

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

C示例查找循环链接列表反向的程序

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

C示例程序创建循环链接列表

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

C示例程序拆分单链表

C示例程序拆分单链表 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *even = NULL; struct node *odd = NULL; struct node *list = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); struct node *current; link->data = data; link->next = NULL; if(list == NULL) { list = link; return; } current = list; while(current->next!=NULL) current = current->next; // Insert link at the end of the list current->next = link; } void display(struct node *head) { struct node *ptr = head; printf("[head] =>"); //start from the beginning while(ptr != NULL) { printf(" %d =>",ptr->data); ptr = ptr->next; } printf(" [null]\n"); } void split_list() { // Allocate memory for new node; struct node *link; struct node *current; while(list != NULL) { struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = list->data; link->next = NULL; if(list->data%2 == 0) { if(even

阅读全文 ↗
C

C示例组合链接列表的程序

C示例组合链接列表的程序 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *even = NULL; struct node *odd = NULL; struct node *list = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); struct node *current; link->data = data; link->next = NULL; if(data%2 == 0) { if(even == NULL) { even = link; return; } else { current = even; while(current->next != NULL) current = current->next; // Insert link at the end of the list current->next = link; } } else { if(odd == NULL) { odd = link; return; } else { current = odd; while(current->next!=NULL) current = current->next; // Insert link at the end of the list current->next = link; } } } void display(struct node *head) { struct node *ptr = head; printf("[head] =>"); while(ptr != NULL) { printf(" %d =>",ptr->data); ptr = ptr->next; } printf(" [null]\n"); } void combine() { struct node *link; list = even; link = list; wh

阅读全文 ↗
C

C示例程序删除单链表中的项目

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

C示例程序更新单链表中的项目

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

C示例在单一链接列表中查找项目的程序

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

阅读全文 ↗