编程札记

数

数据结构在给定的索引数组后插入

数据结构在给定的索引数组后插入 #include <stdio.h> #define MAX 5 void main() { int array[MAX] = {1, 2, 4, 5}; int N = 4; // number of elements in array int i = 0; // loop variable int index = 1; // index location after which value will be inserted int value = 3; // new data element to be inserted // print array before insertion printf("Printing array before insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d \n", i, array[i]); } // now shift rest of the elements downwards for(i = N; i >= index + 1; i--) { array[i + 1] = array[i]; } // add new element at first position array[index + 1] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d\n", i, array[i]); } }

阅读全文 ↗
数

数据结构插入给定索引数组

数据结构插入给定索引数组 #include <stdio.h> #define MAX 5 void main() { int array[MAX] = {1, 2, 4, 5}; int N = 4; // number of elements in array int i = 0; // loop variable int index = 2; // index location to insert new value int value = 3; // new data element to be inserted // print array before insertion printf("Printing array before insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d \n", i, array[i]); } // now shift rest of the elements downwards for(i = N; i >= index; i--) { array[i+1] = array[i]; } // add new element at first position array[index] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d\n", i, array[i]); } }

阅读全文 ↗
数

数据结构在数组的开头插入

数据结构在数组的开头插入 #include <stdio.h> #define MAX 5 void main() { int array[MAX] = {2, 3, 4, 5}; int N = 4; // number of elements in array int i = 0; // loop variable int value = 1; // new data element to be stored in array // print array before insertion printf("Printing array before insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d \n", i, array[i]); } // now shift rest of the elements downwards for(i = N; i >= 0; i--) { array[i+1] = array[i]; } // add new element at first position array[0] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d\n", i, array[i]); } }

阅读全文 ↗
数

数据结构插入操作

数据结构插入操作 #include <stdio.h> main() { int LA[] = {1,3,5,7,8}; int item = 10, k = 3, n = 5; int i = 0, j = n; printf("The original array elements are :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } n = n + 1; while( j >= k) { LA[j+1] = LA[j]; j = j - 1; } LA[k] = item; printf("The array elements after insertion :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } }

阅读全文 ↗
C

C示例拆分双向链表

C示例拆分双向链表 #include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *list = NULL; struct node *list_last = NULL; struct node *even = NULL; struct node *even_last = NULL; struct node *odd = NULL; struct node *odd_last = 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->prev = NULL; link->next = NULL; // If head is empty, create new list if(list == NULL) { list = link; return; } current = list; // 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; list_last = link; link->prev = current; } //display the list void print_backward(struct node *head) { struct node *ptr = head; printf("\n[last] <=>"); //start from the beginning while(ptr != NULL) { printf(" %d <

阅读全文 ↗
C

C示例组合两个双向链表

C示例组合两个双向链表 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *list = NULL; struct node *list_last = NULL; struct node *even = NULL; struct node *even_last = NULL; struct node *odd = NULL; struct node *odd_last = 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->prev = NULL; link->next = NULL; // If head is empty, create new list 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; even_last = link; link->prev = current; } } 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; odd_last = link; link->prev

阅读全文 ↗
C

C示例从双向链接列表中删除数据

C示例从双向链接列表中删除数据 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *head = NULL; struct node *last = NULL; struct node *current = NULL; //display the list void printList() { struct node *ptr = head; printf("\n[head] <=>"); //start from the beginning while(ptr != NULL) { printf(" %d <=>",ptr->data); ptr = ptr->next; } printf(" [last]\n"); } void print_backward() { struct node *ptr = last; printf("\n[last] <=>"); //start from the beginning while(ptr != NULL) { printf(" %d <=>",ptr->data); ptr = ptr->prev; } printf(" [head]\n"); } //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->prev = NULL; 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

阅读全文 ↗
C

C示例更新双向链接列表中的数据

C示例更新双向链接列表中的数据 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *head = NULL; struct node *last = NULL; struct node *current = NULL; //display the list void printList() { struct node *ptr = head; printf("\n[head] <=>"); //start from the beginning while(ptr != NULL) { printf(" %d <=>",ptr->data); ptr = ptr->next; } printf(" [last]\n"); } //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->prev = NULL; 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; last = link; link->prev = current; } void update_data(int old, int new) { int pos = 0; if(head == NULL) { printf("Linked List not initialized"); return; } cur

阅读全文 ↗
C

C示例程序搜索双链表

C示例程序搜索双链表 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *head = NULL; struct node *last = NULL; struct node *current = NULL; //display the list void printList() { struct node *ptr = head; printf("\n[head] <=>"); //start from the beginning while(ptr != NULL) { printf(" %d <=>",ptr->data); ptr = ptr->next; } printf(" [last]\n"); } //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->prev = NULL; 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; last = link; link->prev = current; } void search(int data) { int pos = 0; if(head==NULL) { printf("Linked List not initialized"); return; } current = head; while(c

阅读全文 ↗
C

C示例程序查找双向链表的大小

C示例程序查找双向链表的大小 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *head = NULL; struct node *last = NULL; struct node *current = NULL; //display the list void printList() { struct node *ptr = head; printf("\n[head] <=>"); //start from the beginning while(ptr != NULL) { printf(" %d <=>",ptr->data); ptr = ptr->next; } printf(" [last]\n"); } //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->prev = NULL; 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; last = link; link->prev = current; } void sizeof_list() { int size = 0; struct node *link; if(head==NULL) { printf("\n Size of list is %d\n", size); return; }

阅读全文 ↗
C

C示例以反向显示双向链接列表

C示例以反向显示双向链接列表 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *head = NULL; struct node *last = NULL; struct node *current = NULL; //display the list void printList() { struct node *ptr = head; printf("\n[head] <=>"); //start from the beginning while(ptr != NULL) { printf(" %d <=>",ptr->data); ptr = ptr->next; } printf(" [last]\n"); } //display the list void print_backward() { struct node *ptr = last; printf("\n[head] <=>"); //start from the beginning while(ptr != NULL) { printf(" %d <=>",ptr->data); ptr = ptr->prev; } printf(" [last]\n"); } //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->prev = NULL; 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; // Inser

阅读全文 ↗
C

C示例创建双向链接列表

C示例创建双向链接列表 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *head = NULL; struct node *last = 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->prev = NULL; 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; last = link; link->prev = current; } //display the list void printList() { struct node *ptr = head; printf("\n[head] <=>"); //start from the beginning while(ptr->next != NULL) { 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); printL

阅读全文 ↗