📄 mynode.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "mynode.h"/* Function to create an empty list (the void argument list indicates that it takes no arguments) */node_ptr create(void){ node_ptr list = (node_ptr) malloc(sizeof(node_ptr)); list->to_vertex = 0;/* superfluous initialisation */ list->next = NULL;/* necessary initialisation to indicate last node */ return list;}/* Function to return dynamically allocated memory in list */void destroy(node_ptr list){ /* Note that we don't really need to have the variable "current" here, as we could just advance list itself if we preferred. (Some of the other list functions also make use of an additional such variable.) */ node_ptr current = list; while(current) { node_ptr to_free = current; current = current->next; free(to_free); } return; /* Note that the simpler: free(current); current = current->next; makes the mistake of using dynamically allocated memory after it's been freed */ }/* Function to insert n at front of list */void insert_at_front(int n, node_ptr list){ node_ptr new_node = (node_ptr) malloc(sizeof(struct node)); new_node->to_vertex = n; new_node->next = list->next; list->next = new_node; return;}/* Function to print list */void print(node_ptr list){ node_ptr current = list->next; while(current) { printf("%d ", current->to_vertex); current = current->next; } printf("\n"); return;}/* Function to insert n in (non-decreasing) order in list - assuming list items are already in (non-decreasing) order. */void insert_in_order_node(int n, node_ptr list){ node_ptr before = list; node_ptr new_node = (node_ptr) malloc(sizeof(node_ptr)); new_node->to_vertex = n; /* Move along list until right place is found, looking for node after which new node should go */ while(before->next && (before->next->to_vertex < n)) { before = before->next; } new_node->next = before->next; before->next = new_node; return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -