C/C+|28-20書き出しプログラムチェーンテーブルのすべてのノードを削除

1453 ワード

/*
         
*/
#include 
#include 
#include 
#include 
#include   
#include 
#include 
#include 
#include 

using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode;

LNode *CreatLink(int num)  //   
{
	int i=1;
	LNode *head, *tail, *p;
	head = (LNode *)malloc(sizeof(LNode));
	tail = head;
	while (num--)
	{
		p = (LNode *)malloc(sizeof(LNode));
		p->data = i;
		tail->next = p;
		tail = p;
		i++;
	}
	tail->next = NULL;
	return head;
}

LNode *FindLink(LNode *head,int X)  //  
{
	assert( head != NULL);
	LNode *chick=head->next;
	while (chick != NULL)
	{
		if (chick->data == X)
			return chick;
		else
			chick = chick->next;
	}
	return NULL;
}

void DeleteLink(LNode *head, int X)  //  
{
	assert(head != NULL);
	LNode *p;
	LNode *q;
	p=FindLink(head, X);
	q = p->next;
	p->data = q->data;
	p->next = q->next;
	free(q);
}

void DeleteAllLink(LNode *head)
{
	assert(head != NULL);
	LNode *p ;
	while (head!=NULL)
	{
		p = head->next;
		free(head);
		head = p;
	}
	cout << "all Linked Deleted!" << endl;  //   head  NULL
}

int main()
{
	LNode *head = CreatLink(50);
	DeleteLink(head, 5);
	DeleteAllLink(head);
	system("pause");//  head     
	return 0;
}