加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C/C++ 单链表

发布时间:2020-12-15 04:46:50 所属栏目:百科 来源:网络整理
导读:#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 *n

#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);

}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读