今天写了个链表,好累。

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

#define TRUE 1
#define FALSE 0
#define EXIT 0

//定义结点
struct nodetype 
{
	int data;
	struct nodetype *next;
};

typedef struct nodetype *node;
//判断链表是否为空
int IsEmpty(node L)
{
	if(L->next==NULL)
		return TRUE;
	return FALSE;
}
//初始化链表
node ListInit(void)
{
	node L;
	L=(node) malloc ( sizeof(struct nodetype) );
	if(L==NULL)
		printf("Out of memory!\n");
	L->data=0;
	L->next=NULL;

	return L;
}
//获取第i个结点的数值
int GetNode(node L,int i)
{
	int count;
	node p;

	if( IsEmpty(L) ){
		printf("List is empty!\n");
		return INT_MAX;
	}
	else if(i<=0 || i>L->data){
		printf("Insert position is ERRO! Plsease retry.\n");
		return INT_MAX;
	}
	else{
		for(count=0,p=L; count<i; count++)
			p=p->next;
		return p->data;
	}
}
//在第i个结点后插入数值x
void NodeInsert(node L,int i,int x)
{
	node p,p0;
	int count;

	if(i<0 || i>L->data)
		printf("Insert position is ERRO! Plsease retry.\n");
	else{
		for(count=0,p=L; count<i; count++)
			p=p->next;

		p0=(node) malloc ( sizeof(struct nodetype) );
		if(p0==NULL)
			printf("Out of memory!\n");
		else{
			p0->data=x;
			p0->next=p->next;
			p->next=p0;
			L->data++;

			if(i!=0)
				printf("%d was been inserted after Node-%d.\n",x,i);
			else	
				printf("%d was been inserted after Head.\n",x);
		}
	}
}
//删除第i个结点
void NodeDelete(node L,int i)
{
	int count,x;
	node p,pre;

	if( IsEmpty(L) )
		printf("List is empty!\n");

	else if(i<=0 || i>L->data)
		printf("Insert position is ERRO! Please retry.\n");

	else{	
		for(count=0,p=L; count<i; count++){
			pre=p;
			p=p->next;
		}
		x=p->data;
		pre->next=p->next;
		free(p);
		L->data--;
		printf("Node-%d=%d has beed deleted.\n",i,x);
	}
}
//遍历链表
void ListTraverse(node L)
{
	node p=L->next;

	if(p==NULL)
		printf("List is empty.\n");
	else{
		printf("List_number=%d: ",L->data);

		while(p!=NULL){	
			printf("%d ",p->data);
			p=p->next;
		}
	}
	printf("\n");
}
//销毁链表
void ListDestroy(node L)
{
	int n=L->data;
	
	if(n==0)
		printf("List is empty.\n");
	while(n!=0){
		NodeDelete(L,n);
		n--;
	}
	L->data=0;
	L->next=NULL;
}
//打印菜单
void MenuPrint(void)
{
	printf("=======================\n");
	printf("1.Initialize List\n");
	printf("2.Insert x after Node-i\n");
	printf("3.Delete Node-i\n");
	printf("4.Get Node-i\n");
	printf("5.If List is empty\n");
	printf("6.Traverse List\n");
	printf("7.Destroy List\n");
	printf("0.Exit!\n");
	printf("=======================\n");
	printf("\n");
}



//主函数
int main(void)
{
	int i,x;
	int input=1;
	node list;
	//输入选项
	while(input!=EXIT){
		MenuPrint();
		printf("Choose >>>: ");
		scanf("%d",&input);
		if(input<0 || input>7){
			printf("\n");
			printf("Your choice is ERRO!\n");
			printf("\n");
		}
		//处理选项
		switch(input)
		{
			case 1:	
				list=ListInit();
				printf("List has been initialized.\n");
				printf("\n");
				break;
			case 2:	
				printf("Input i x: ");
				scanf(" %d %d",&i,&x);
				printf("\n");
				NodeInsert(list,i,x);
				printf("\n");
				break;
			case 3:
				printf("Input i: ");
				scanf(" %d",&i);
				printf("\n",i);
				NodeDelete(list,i);
				printf("\n");
				break;
			case 4:
				printf("Input i: ");
				scanf(" %d",&i);
				printf("\n");
				printf("Node-%d=%d\n",i,GetNode(list,i));
				printf("\n");
				break;
			case 5:
				if(IsEmpty(list))
					printf("Yes.It is.\n");
				else
					printf("No.It is not.\n");
				printf("\n");
				break;
			case 6:
				ListTraverse(list);
				printf("\n");
				break;
			case 7:	
				ListDestroy(list);
				printf("List has been destroyed.\n");
				printf("\n");
				break;
			case 0:
				input=0;
		}
	}

	system("PAUSE");
	return 0;
}


 说明:(1) 第一步务必输入:1, 即:1.Initialize List 分配表头。

             (2) 在使用时请输入数字对应菜单选项,不要使用字母,否则会出错。

阅读更多
个人分类: 算法导论-程序
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭