表http://www.cnblogs.com/vamei/archive/2013/03/14/2958940.html
#include <stdio.h>
#include <stdlib.h>
typedef struct node *LIST;
typedef struct node *position;
/* node,节点 */
struct node {
int element;
position next;
};
LIST init_list(void);
void print_list(LIST);
void insert_node(position, int);
position find_last(LIST);
position find_value(LIST, int);
position find_previous(LIST, position);
void delete_node(LIST, position);
void main()
{
LIST L;
position np;
int i;
/* elements to be put into the list */
int a[] = {1, 3, 5, 7, 9};
/* initiate a list */
L = init_list();
print_list(L);
/* insert nodes. Insert just after head node */
for (i=4; i>=0; i--) {
insert_node(L, a[i]);
}
print_list(L);
np = find_value(L, 5);
printf("删除节点地址%p\n",np);
delete_node(L, np); //重点在这里,这里面的previous没看懂
print_list(L);
}
LIST init_list(void)
{
LIST L;
L = (position) malloc(sizeof(struct node));
L->next = NULL;
return L;
}
void print_list(LIST L)
{
position np;
if(is_null(L)) {
printf("Empty List\n\n");
return;
}
np = L;
while(np->next != NULL) {
np = np->next;
printf("%p: %d ", np, np->element);
}
printf("\n");
}
/*
* if a list only has head node, then the list is null.
* 判断表是否为空
*/
int is_null(LIST L)
{
return ((L->next)==NULL);
}
void insert_node(position np, int value)
{
position nodeAddr;
nodeAddr = (position) malloc(sizeof(struct node));
nodeAddr->element = value;
nodeAddr->next = np->next;
np->next = nodeAddr; // 这一行的作用
}
/*
* Delete all nodes in a list
* 删除表
*/
void delete_list(LIST L)
{
position np, next; //为什么还要next position
np = L;
do {
next = np->next;
free(np);
np = next;
} while(next != NULL);
}
/*
* find the first node with specific value
* 查询
*/
position find_value(LIST L, int value)
{
position np;
np = L;
while (np->next != NULL) {
np = np->next;
// printf("%p\n",np);
// printf("%p\n",np->next);
if (np->element == value) return np;
}
return NULL;
}
void delete_node(LIST L, position np)
{
position previous, next;
next = np->next;
previous = find_previous(L, np);
if(previous != NULL) {
previous->next = next;
free(np);
}
else {
printf("Error: np not in the list");
}
}
/*
* find the last node of the list
* 寻找表的最后一个节点
*/
position find_last(LIST L)
{
position np;
np = L;
while(np->next != NULL) {
np = np->next;
}
return np;
}
/*
* This function serves for 2 purposes:
* 1. find previous node
* 2. return NULL if the position isn't in the list
* 寻找npTarget节点前面的节点
*/
position find_previous(LIST L, position npTarget)
{
position np;
np = L;
while (np->next != NULL) {
if (np->next == npTarget) return np;
np = np->next;
}
return NULL;
}
图 http://www.cnblogs.com/vamei/p/3113912.html
#include <stdio.h>
#include <stdlib.h>
#define NUM_V 5
typedef struct node *position;
/* node */
struct node {
int element;
position next;
};
/*
* operations (stereotype)
*/
void insert_edge(position, int, int);
void print_graph(position graph, int nv);
/* for testing purpose */
void main()
{
struct node graph[NUM_V]; // NUM_V 这里是5 ,define
int i;
// initialize the vertices
for(i=1; i<NUM_V; i++) {
(graph+i)->element = i;
(graph+i)->next = NULL;
}
// insert edges
insert_edge(graph,1,2);
insert_edge(graph,1,4);
insert_edge(graph,3,2);
insert_edge(graph,4,2);
insert_edge(graph,4,3);
print_graph(graph,NUM_V);
}
/* print the graph */
void print_graph(position graph, int nv) {
int i;
position p;
for(i=1; i<nv; i++) {
p = (graph + i)->next;
printf("From %3d: ", i);
while(p != NULL) {
printf("%d->%d; ", i, p->element);
p = p->next;
}
printf("\n");
}
}
/*
* insert an edge
*/
void insert_edge(position graph,int from, int to)
{
position np;
position nodeAddr;
np = graph + from; //np 的含义是什么。 graph+1,graph+2的意思
printf("%p",np);
nodeAddr = (position) malloc(sizeof(struct node));
nodeAddr->element = to;
nodeAddr->next = np->next; // 这两段的含义是什么 相当于 graph+1→next赋值给 nodeaddr→next,
np->next = nodeAddr; // 右移的含义吗
}