C言語一方向チェーンテーブル

3297 ワード

/* 1.C       
 * 2.   ( , , ,  ) 
 * Author: Mr.Long 
 * Date  : 2015-12-1 15:24:26 
 */
#include
#include

/*      */
struct node {
	int data;
	node *next;
};

/*      */
void printNode(struct node *head);
struct node* appendNode(struct node *head,int);
struct node* addFirstNode(struct node *head,int);
struct node* findNode(struct node *head,int);
int deletNode(struct node *head,int);

int main() {
	
	/*     head  ,        */
	struct node *head = NULL;
	//       
	head = (struct node *)malloc(sizeof(struct node));
	//         
	head->data = 0;
	head->next = NULL;

	printf("*****************    ******************\r
"); /* */ head = appendNode(head,5); head = appendNode(head,8); head = appendNode(head,4); head = appendNode(head,34); head = appendNode(head,6); head = appendNode(head,9); /* */ head = addFirstNode(head,12); head = addFirstNode(head,61); printf(" ...
"); printf("***************** ******************\r
"); /* */ struct node *a; a = findNode(head,34); if(a != NULL) { printf(" ...
"); printf("data:%d
",a->data); printf("next:0x%X
",a->next); } else { printf("
"); } printf("***************** ******************\r
"); /* 1 0 */ int dest = 0; bool state = deletNode(head,dest); if(state) printf(" [%d] ...
",dest); else printf(" [%d] ...
",dest); printf("***************** ******************\r
"); /* */ printNode(head); return 0; } int deletNode(struct node *head,int dest) { struct node *p = head; struct node *lp; while(p->next) { if(p->data == dest) { // : next next lp->next = p->next; // free(p); return true; } // lp = p; // p = p->next; } return false; } struct node* findNode(struct node *head,int dest) { struct node *p = head; struct node *pc = NULL;// while(p) { // p NULL if(p->data == dest) {// pc = p; break; } p = p->next; // } return pc; } struct node* appendNode(struct node *head,int data) { if(data == NULL) return head; if(head == NULL) { // , printf("head is NULL
"); head = (struct node *)malloc(sizeof(struct node)); head->data = data; head->next = NULL; return head; } /* * p->next p * p NULL * p NULL 56 */ struct node *p = head; while(p->next) { p = p->next; } struct node *pNew; pNew = (struct node *)malloc(sizeof(struct node)); pNew->data = data; pNew->next = NULL; p->next = pNew; /* : * p * 47 while p */ return head; } struct node* addFirstNode(struct node *head,int data) { struct node *p; p = (struct node *)malloc(sizeof(struct node)); p->data = data; p->next = head; return p; } void printNode(struct node *head) { struct node *p = head; while(p) { /* p NUll */ printf("self=0x%X | data=%2d | next=0x%X
",p,p->data,p->next); p = p->next; } }