例134:删除链表元素
#include <stdio.h>
#include <malloc.h>
int main(int argc, char const *argv[])
{
int i;
// 定义链表节点结构
struct ListEntry {
int number;
struct ListEntry *next;
} start, *node, *previous;
start.next = NULL;
// 初始化链表
node = &start;
for (i = 1; i <= 10; i++)
{
node->next = (struct ListEntry *) malloc(sizeof(struct ListEntry));
node = node->next;
node->number = i;
node->next = NULL;
}
// 打印链表
node = start.next;
while (node)
{
printf("%d ", node->number); // 1 2 3 4 5 6 7 8 9 10
node = node->next;
}
printf("\n");
// 删除数字5
node = start.next;
previous = &start;
while (node)
if (node->number == 5)
{
// 前一个节点的next指向删除节点的下一个节点
previous->next = node->next;
free(node);
break;
}
else
{
// 指向下一个节点,继续遍历
node = node->next;
previous = previous->next;
}
// 打印链表
node = start.next;
while (node)
{
printf("%d ", node->number); // 1 2 3 4 6 7 8 9 10
node = node->next;
}
return 0;
}
例135:删除双链表的元素
#include <stdio.h>
#include <malloc.h>
int main(int argc, char const *argv[])
{
int i, found;
// 定义双向链表节点结构
struct ListEntry {
int number;
struct ListEntry *next;
struct ListEntry *previous;
} start, *node;
start.next = NULL;
start.previous = NULL;
// 初始化链表
node = &start;
for (i = 1; i <= 10; i++)
{
node->next = (struct ListEntry *) malloc(sizeof(struct ListEntry));
node->next->previous = node;
node = node->next;
node->number = i;
node->next = NULL;
}
// 删除数字7
node = start.next;
found = 0;
do {
if (node->number == 7)
{
found = 1;
// 删除元素前一个节点的next指向下一个节点
node->previous->next = node->next;
// 删除元素的下一个节点的previous指向前一个节点
node->next->previous = node->previous;
free(node);
}
else
node = node->next;
} while ((node) && (!found));
// 打印链表
node = start.next;
do {
printf("%d ", node->number);
node = node->next;
} while (node);
return 0;
}
例136:枚举的定义使用1
#include <stdio.h>
int main(int argc, char const *argv[])
{
// 定义枚举类型weekdays并定义初始化枚举变量
enum weekdays { Monday = 10, Tuesday = 20,
Wednesday = 30, Thursday = 40,
Friday = 50 };
printf("%d %d %d %d %d\n", Monday, Tuesday, Wednesday, Thursday, Friday); // 10 20 30 40 50
return 0;
}
例137:枚举的定义使用2
#include <stdio.h>
int main(int argc, char const *argv[])
{
// // 定义枚举类型weekdays并定义枚举变量(默认值初始化)
enum weekdays { Monday, Tuesday, Wednesday, Thursday, Friday};
printf("%d %d %d %d %d\n", Monday, Tuesday, Wednesday, Thursday, Friday); // 0 1 2 3 4
return 0;
}
例138:顺序打印指定目录下的文件夹名
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
DIR *directory_pointer;
struct dirent *entry;
// 定义文件链表结构
struct FileList {
char filename[64];
struct FileList *next;
} start, *node, *previous, *new;
// 打开文件夹错误
if ((directory_pointer = opendir(argv[1])) == NULL)
printf("Error opening %s\n", argv[1]);
else
{
start.next = NULL;
while (entry = readdir(directory_pointer))
{
previous = &start;
node = start.next;
// 排序
while ((node) && (strcmp(entry, node->filename) > 0))
{
node = node->next;
previous = previous->next;
}
new = (struct FileList *) malloc(sizeof(struct FileList));
if (new == NULL)
{
printf("Insufficient memory to store list\n");
exit(1);
}
new->next = node;
previous->next = new;
strcpy(new->filename, entry);
}
closedir(directory_pointer);
// 打印文件夹链表
node = start.next;
while (node)
{
printf("%s\n", node->filename);
node = node->next;
}
}
return 0;
}
例139:创建子进程1
#include <process.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("About to call child process\n\n");
spawnl(P_WAIT, "CHILD.EXE", "CHILD.EXE", "AAA", "BBB", "CCC", NULL);
printf("\n\nBack from child process\n");
return 0;
}
例140:创建子进程2
#include <process.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
char *env[] = {"FILE=SPAWNLXX.C", "LANGUAGE=C", "OS=DOS", NULL};
spawnle(P_WAIT, "CHILD.EXE", "CHILD.EXE", "Using-spawnle", "BBB", NULL, env);
spawnlp(P_WAIT, "CHILD.EXE", "CHILD.EXE", "Using-spawnlp", "BBB", NULL);
spawnlpe(P_WAIT, "CHILD.EXE", "CHILD.EXE", "Using-spawnlpe", "BBB", NULL, env);
return 0;
}
例141:创建子进程3
#include <stdio.h>
#include <process.h>
int main(int argc, char const *argv[])
{
char *env[] = {"FILENAME=SPAWNVXX.C", "OS=DOS", "ROUTINES=SPAWNVXX", NULL };
char *argv[] = { "CHILD.EXE", "AAA", "BBB", NULL };
spawnv(P_WAIT, "CHILD.EXE", argv);
spawnve(P_WAIT, "CHILD.EXE", argv, env);
spawnvp(P_WAIT, "CHILD.EXE", argv);
spawnvpe(P_WAIT, "CHILD.EXE", argv, env);
return 0;
}
例142:查看当前路径文件目录
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
if (system("DIR"))
printf("Error invoking DIR\n");
return 0;
}
例143:想想下面代码正确吗?
#include <stdio.h>
int value;
int main(int argc, char const *argv[])
{
printf("%d\n", value);
return 0;
}
int value = 1001;
例144:枚举遍历
#include <stdio.h>
int main(int argc, char const *argv[])
{
enum { Monday, Tuesday, Wednesday, Thursday, Friday } day;
// 遍历
for (day = Monday; day <= Friday; day++)
if (day == Monday)
printf("No fun---meetings all day Monday\n");
else if (day == Tuesday)
printf("No fun---do Monday's work today\n");
else if (day == Wednesday)
printf("Hump day...");
else if (day == Thursday)
printf("Schedule meetings for next Monday\n");
else
printf("Meet everyone at happy hour!\n");
return 0;
}

这个示例展示了如何在C语言中实现链表的基本操作,包括删除元素、初始化和打印链表。此外,还涉及到了子进程的创建以及枚举类型的使用。代码中包含了单链表和双向链表的元素删除,以及枚举类型的定义和遍历。最后,还演示了如何通过系统调用查看当前目录的文件夹名。
814

被折叠的 条评论
为什么被折叠?



