新建 文本文档.txt

来自「简单的链表实现 链表可以初步了解」· 文本 代码 · 共 91 行

TXT
91
字号
#include <stdio.h> 
#include <string.h> 
#include <malloc.h> 

typedef struct _ChNode 
{ 
int ch; 
struct _ChNode *next; 
} ChNode; 

ChNode* cncreate(const char *str) 
{ 
ChNode *head = NULL, *tail, *node; 

while (*str) 
{ 
node = (ChNode *)malloc(sizeof(ChNode)); 
node->ch = *str; 
node->next = NULL; 
if (head == NULL) 
{ 
head = node; 
tail = node; 
} 
tail->next = node; 
tail = node; 
str++; 
} 
return head; 
} 

ChNode* cnchr(ChNode *node, int ch) 
{ 
while (node) 
{ 
if (node->ch == ch) 
{ 
return node; 
} 
node = node->next; 
} 
return NULL; 
} 

ChNode* cntail(ChNode *node) 
{ 
while (node->next) 
{ 
node = node->next; 
} 
return node; 
} 

void cnins(ChNode *a, ChNode *b) 
{ 
ChNode *btail = cntail(b); 
btail->next = a->next; 
a->next = b; 
} 

void cncat(ChNode *a, ChNode *b, int ch) 
{ 
ChNode *node = cnchr(a, ch); 
if (node == NULL) 
{ 
node = cntail(a); 
} 
cnins(node, b); 
} 

void cnprint(ChNode *node) 
{ 
while (node) 
{ 
printf("%c", node->ch); 
node = node->next; 
} 
printf("\n"); 
} 

void main() 
{ 
ChNode *list1, *list2; 

list1 = cncreate("show me the money!"); 
cnprint(list1); 
list2 = cncreate("give me five!"); 
cnprint(list2); 
cncat(list1, list2, 'w'); 
cnprint(list1); 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?