時間漸漸緊張,只能在閒暇時間裡寫寫這類的小程序,不斷鍛煉編程的思路:
compera_list.h:
[cpp]
#ifndef __COMPERA_LIST_H__
#define __COMPERA_LIST_H__
/*
* list鏈表邊界值設定
*/
#define MAXSIZE_A 2
#define MAXSIZE_B 3
#define MAXSIZE_C 6
#include <stdio.h>
#include <stdlib.h>
/*
* list可支持方法合集結構體
*/
struct list_operations;
/*
* list結構體
*/
struct list_str {
unsigned char *name;
unsigned int values;
unsigned int private_cnt;
struct list_str *next;
struct list_operations *l_ops;
};
/*
* list可支持方法
*/
struct list_operations {
void (*init_list)(struct list_str *pList, int num_nodes);
void (*sort_by_inc)(struct list_str *pList, int num_nodes);
void (*print_list)(struct list_str *pList);
};
/*
* 可提供的外部接口API
*/
struct list_str *register_list(struct list_str *pList, int num_nodes, unsigned char *name);
struct list_str *compera_list(struct list_str *pList1, struct list_str *pList2, struct list_str *pList3);
#endif
#ifndef __COMPERA_LIST_H__
#define __COMPERA_LIST_H__
/*
* list鏈表邊界值設定
*/
#define MAXSIZE_A 2
#define MAXSIZE_B 3
#define MAXSIZE_C 6
#include <stdio.h>
#include <stdlib.h>
/*
* list可支持方法合集結構體
*/
struct list_operations;
/*
* list結構體
*/
struct list_str {
unsigned char *name;
unsigned int values;
unsigned int private_cnt;
struct list_str *next;
struct list_operations *l_ops;
};
/*
* list可支持方法
*/
struct list_operations {
void (*init_list)(struct list_str *pList, int num_nodes);
void (*sort_by_inc)(struct list_str *pList, int num_nodes);
void (*print_list)(struct list_str *pList);
};
/*
* 可提供的外部接口API
*/
struct list_str *register_list(struct list_str *pList, int num_nodes, unsigned char *name);
struct list_str *compera_list(struct list_str *pList1, struct list_str *pList2, struct list_str *pList3);
#endif
compera_list.c:
[cpp]
#include "compera_list.h"
/*
* 初始化鏈表節點函數;
* pList ---> 傳入鏈表
* num_nodes ---> 鏈表節點數
*/
void init_list(struct list_str *pList, int num_nodes)
{
int i;
struct list_str *temp = pList;
printf("輸入%s鏈表節點值:\n", pList->name);
for (i = 0; i < num_nodes; i++) {
printf("第%d個節點值: ", i+1);
scanf("%d", &pList->values);
pList->next = ++temp;
pList->private_cnt = num_nodes;
if (i != num_nodes - 1) {
pList++;
}
}
pList->next = NULL;
printf("\n");
}
/*
* 節點數據交換函數;
* i ---> 前級數據
* j ---> 後繼數據
*/
void values_switch(int *i, int *j)
{
int temp;
temp = *i;
*i = *j;
*j = temp;
}
/*
* 遞增排序函數;
* pList ---> 傳入鏈表
* num_nodes ---> 鏈表節點數
*/
void sort_by_inc(struct list_str *pList, int num_nodes)
{
struct list_str *pTemp = pList;
while (pList->next) {
while (pTemp->next) {
pTemp = pTemp->next;
if (pList->values > pTemp->values) {
values_switch(&pList->values, &pTemp->values);
} else {
continue;
}
}
pList = pList->next;
pTemp = pList; // 重置pTemp指針指向單元
}
}
/*
* 打印節點數據函數;
* pList ---> 傳入鏈表
*/
void print_list(struct list_str *pList)
{
unsigned int i;
printf("%s節點值打印:\n", pList->name);
for (i = 0; (pList != NULL) && (i < pList->private_cnt); i++) {
printf("第%d節點值為: %d\n", i+1, pList->values);
pList = pList->next;
}
printf("\n");
}
struct list_operations list_ops = {
.init_list = init_list,
.sort_by_inc = sort_by_inc,
.print_list = print_list,
};
/*
* 注冊結構體函數;
* pList ---> 傳入鏈表
* num_nodes ---> 鏈表節點數
* name ---> 鏈表名字
*/
struct list_str *register_list(struct list_str *pList, int num_nodes, unsigned char *name)
{
pList = (struct list_str *)malloc(sizeof(struct list_str) * num_nodes);
pList->private_cnt = num_nodes;
pList->name = name;
pList->l_ops = &list_ops;
return pList;
}
/*
* 合並鏈表函數;
* pList1 ---> 傳入鏈表1
* pList2 ---> 傳入鏈表2
* pList3 ---> 傳入鏈表3
*/
struct list_str *compera_list(struct list_str *pList1, struct list_str *pList2, struct list_str *pList3)
{
unsigned int i;
struct list_str *temp = pList3;
struct list_str *pRet = pList3;
/*
* 組合鏈表
*/
for (i = 0; i < pList1->private_cnt; i++)
{
pList3->values = pList1->values;
pList3++;
if (NULL != pList1->next) {
pList1++;
}
temp->next = pList3;
temp = temp->next;
}
for (i = 0; i < pList2->private_cnt; i++)
{
pList3->values = pList2->values;
pList3++;
if (NULL != pList2->next) {
pList2++;
} else {
break;
}
temp->next = pList3;
temp = temp->next;
}
temp->next = NULL;
/*
* 指針歸位操作
*/
pList3 = pRet;
return pList3;
}
#include "compera_list.h"
/*
* 初始化鏈表節點函數;
* pList ---> 傳入鏈表
* num_nodes ---> 鏈表節點數
*/
void init_list(struct list_str *pList, int num_nodes)
{
int i;
struct list_str *temp = pList;
printf("輸入%s鏈表節點值:\n", pList->name);
for (i = 0; i < num_nodes; i++) {
printf("第%d個節點值: ", i+1);
scanf("%d", &pList->values);
pList->next = ++temp;
pList->private_cnt = num_nodes;
if (i != num_nodes - 1) {
pList++;
}
}
pList->next = NULL;
printf("\n");
}
/*
* 節點數據交換函數;
* i ---> 前級數據
* j ---> 後繼數據
*/
void values_switch(int *i, int *j)
{
int temp;
temp = *i;
*i = *j;
*j = temp;
}
/*
* 遞增排序函數;
* pList ---> 傳入鏈表
* num_nodes ---> 鏈表節點數
*/
void sort_by_inc(struct list_str *pList, int num_nodes)
{
struct list_str *pTemp = pList;
while (pList->next) {
while (pTemp->next) {
pTemp = pTemp->next;
if (pList->values > pTemp->values) {
values_switch(&pList->values, &pTemp->values);
} else {
continue;
}
}
pList = pList->next;
pTemp = pList; // 重置pTemp指針指向單元
}
}
/*
* 打印節點數據函數;
* pList ---> 傳入鏈表
*/
void print_list(struct list_str *pList)
{
unsigned int i;
printf("%s節點值打印:\n", pList->name);
for (i = 0; (pList != NULL) && (i < pList->private_cnt); i++) {
printf("第%d節點值為: %d\n", i+1, pList->values);
pList = pList->next;
}
printf("\n");
}
struct list_operations list_ops = {
.init_list = init_list,
.sort_by_inc = sort_by_inc,
.print_list = print_list,
};
/*
* 注冊結構體函數;
* pList ---> 傳入鏈表
* num_nodes ---> 鏈表節點數
* name ---> 鏈表名字
*/
struct list_str *register_list(struct list_str *pList, int num_nodes, unsigned char *name)
{
pList = (struct list_str *)malloc(sizeof(struct list_str) * num_nodes);
pList->private_cnt = num_nodes;
pList->name = name;
pList->l_ops = &list_ops;
return pList;
}
/*
* 合並鏈表函數;
* pList1 ---> 傳入鏈表1
* pList2 ---> 傳入鏈表2
* pList3 ---> 傳入鏈表3
*/
struct list_str *compera_list(struct list_str *pList1, struct list_str *pList2, struct list_str *pList3)
{
unsigned int i;
struct list_str *temp = pList3;
struct list_str *pRet = pList3;
/*
* 組合鏈表
*/
for (i = 0; i < pList1->private_cnt; i++)
{
pList3->values = pList1->values;
pList3++;
if (NULL != pList1->next) {
pList1++;
}
temp->next = pList3;
temp = temp->next;
}
for (i = 0; i < pList2->private_cnt; i++)
{
pList3->values = pList2->values;
pList3++;
if (NULL != pList2->next) {
pList2++;
} else {
break;
}
temp->next = pList3;
temp = temp->next;
}
temp->next = NULL;
/*
* 指針歸位操作
*/
pList3 = pRet;
return pList3;
}
main.c:
[cpp]
#include "compera_list.h"
/*
* 聲明要使用的全局變量:
* *list_A;
* *list_B;
* *list_C;
*/
static struct list_str *list_A;
static struct list_str *list_B;
static struct list_str *list_C;
int main(int argc, char *argv[])
{
unsigned int size_A;
unsigned int size_B;
/*
* size_A: 指定list_A長度
* size_B: 指定list_B長度
*/
printf("輸入A鏈表與B鏈表的長度(如2,3):");
scanf("%d,%d", &size_A, &size_B);
printf("\n");
/*
* 分配指針空間與構建list操作函數成員
*/
list_A = register_list(list_A, size_A, "list_A");
list_B = register_list(list_B, size_B, "list_B");
list_C = register_list(list_C, (size_A + size_B), "list_C");
/*
* 初始化節點值
*/
list_A->l_ops->init_list(list_A, size_A);
list_B->l_ops->init_list(list_B, size_B);
/*
* 按遞增形式排序
*/
list_A->l_ops->sort_by_inc(list_A, size_A);
list_B->l_ops->sort_by_inc(list_B, size_B);
/*
* 組合list_A與list_B
*/
list_C = compera_list(list_A, list_B, list_C);
/*
* 按遞增形式排序
*/
list_C->l_ops->sort_by_inc(list_C, (size_A + size_B));
/*
* 打印節點內容
*/
list_A->l_ops->print_list(list_A);
list_B->l_ops->print_list(list_B);
list_C->l_ops->print_list(list_C);
/*
* 釋放指針空間
*/
free(list_A);
free(list_B);
free(list_C);
return 0;
}
#include "compera_list.h"
/*
* 聲明要使用的全局變量:
* *list_A;
* *list_B;
* *list_C;
*/
static struct list_str *list_A;
static struct list_str *list_B;
static struct list_str *list_C;
int main(int argc, char *argv[])
{
unsigned int size_A;
unsigned int size_B;
/*
* size_A: 指定list_A長度
* size_B: 指定list_B長度
*/
printf("輸入A鏈表與B鏈表的長度(如2,3):");
scanf("%d,%d", &size_A, &size_B);
printf("\n");
/*
* 分配指針空間與構建list操作函數成員
*/
list_A = register_list(list_A, size_A, "list_A");
list_B = register_list(list_B, size_B, "list_B");
list_C = register_list(list_C, (size_A + size_B), "list_C");
/*
* 初始化節點值
*/
list_A->l_ops->init_list(list_A, size_A);
list_B->l_ops->init_list(list_B, size_B);
/*
* 按遞增形式排序
*/
list_A->l_ops->sort_by_inc(list_A, size_A);
list_B->l_ops->sort_by_inc(list_B, size_B);
/*
* 組合list_A與list_B
*/
list_C = compera_list(list_A, list_B, list_C);
/*
* 按遞增形式排序
*/
list_C->l_ops->sort_by_inc(list_C, (size_A + size_B));
/*
* 打印節點內容
*/
list_A->l_ops->print_list(list_A);
list_B->l_ops->print_list(list_B);
list_C->l_ops->print_list(list_C);
/*
* 釋放指針空間
*/
free(list_A);
free(list_B);
free(list_C);
return 0;
}
在娴熟的技術,都源於不斷的訓練。