또 한 번 시계를 썼다

3723 단어
#include 
#include 
#include 

typedef enum{FALSE, TRUE, ERROR} BOOL;

typedef struct _Data
{
	int id;
	char *name;
}Data;

typedef struct _Node
{
	Data data;
	struct _Node *next;
}Node;

Node *CreateHead(Node **node)
{
	if(NULL == node)
	{
		return NULL;
	}
	
	Node *head = (Node *)malloc(sizeof(Node)/sizeof(char));
	if(NULL == head)
	{
		// head 
		return NULL;
	}
	
	head->data.name = (char *)malloc(sizeof(char)*15);
	if(head->data.name == NULL)
	{
		// name 
		free(head);
		return NULL;
	}
	
	head->data.id = 0;
	strcpy(head->data.name,"head");
	head->next = NULL;
	
	*node = head;
	
	return head;
}

BOOL Insert_tail(Node *head, Data data)
{
	if(NULL == head)
	{
		return ERROR;
	}
	
	Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
	if(NULL == node)
	{
		return ERROR;
	}
	node->data.name = (char *)malloc(sizeof(char)*15);
	
	node->data.id = data.id;
	stpcpy(node->data.name, data.name);
	node->next = NULL;
	
	Node *tmp = head;
	while(tmp->next != NULL)
	{
		tmp = tmp->next;
	}
	
	tmp->next = node;
	
	return TRUE;
}

void Print_Data(Data data)
{
	printf("id = %-4d,name = %s
", data.id, data.name); } void Display(Node *head) { if(NULL == head) return ; Node *tmp = head->next; while(tmp != NULL) { Print_Data(tmp->data); tmp = tmp->next; } } BOOL Delete_Data(Node *head, Data data) { if(NULL == head) return ERROR; Node *tmp = head; while(tmp->next != NULL) { if(strcmp(tmp->next->data.name, data.name) == 0) { Node *p = tmp->next; tmp->next = p->next; free(p->data.name); free(p); return FALSE; } tmp = tmp->next; } return TRUE; } BOOL Updata_Data(Node *head, Data data) { if(NULL == head) { return ERROR; } Node *tmp = head; while(tmp->next != NULL) { if(strcmp(tmp->next->data.name, data.name) == 0) { printf(" :"); scanf("%s", tmp->next->data.name); printf("
"); return TRUE; } tmp = tmp->next; } } int Length(Node *head) { if(NULL == head) { return -1; } int length = 0; Node *tmp = head; while(tmp->next != NULL) { length++; tmp = tmp->next; } return length; } void Reverse(Node *head) { if(NULL == head || NULL == head->next || NULL == head->next->next) { return ; } Node *pre = head->next; Node *cur = pre->next; Node *tmp = cur->next; while(cur != NULL) { tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } head->next->next = NULL; head->next = pre; } int main() { Node *head2 = NULL; Node *head1 = CreateHead(&head2); if(NULL == head1) { printf("head1
"); } if(NULL == head2) { printf("head2
"); } Data data1; data1.id = 1; data1.name = (char *)malloc(sizeof(char)*15); strcpy(data1.name, "zoujie"); Data data2; data2.id = 2; char name2[15] = "luzhiwei"; data2.name = name2; Insert_tail(head1, data1); Display(head1); printf("1--------------------------
"); Insert_tail(head1, data2); Display(head1); printf("length = %d
", Length(head1)); printf("2--------------------------
"); Reverse(head1); Display(head1); printf("3--------------------------
"); Delete_Data(head1, data1); Display(head1); printf("4--------------------------
"); Updata_Data(head1, data2); Display(head1); printf("length = %d
", Length(head1)); printf("5--------------------------
"); return 0; }

좋은 웹페이지 즐겨찾기