C/C++ 单链表

#include

#include

typedef int Item;

typedef struct node

{

Item data;

struct node *next;

}Node;

Node *first = NULL;

static void terminate(const char *message)

{

printf("%sn",message);

exit(EXIT_FAILURE);

}

void add_to_list(Item x)

{

Node *new_node;

new_node = (Node *)malloc(sizeof(Node));

if (new_node == NULL)

terminate("Error: malloc Failed in add_to list!");

new_node->data = x;

new_node->next = first;

first = new_node;

}

void delete_element(Item x) {

Node *prev,*cur;

for (cur = first,prev = NULL;

cur != NULL && cur->data != x;

prev = cur,cur = cur->next);

if (cur == NULL)

printf("data was not found!");

if (prev == NULL)

first = first->next;

else

{

prev->next = cur->next;

free(cur);

}

}

void select_list(Item x) {

Node *p;

for (p = first; p != NULL && p->data != x; p = p->next);

if (p == NULL)

printf("Don't find node!");

else

printf("find node!");

}

void print_list()

{

Node *p;

for (p = first; p != NULL; p = p->next) {

printf(" %d ",p->data);

}

}

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...