微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐