《数据结构c语言版》之循环链表的表示和实现

创建一个工程
1.新建status.h文件

#ifndef STATUS_H
#define STATUS_H
//函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1    //不可实行的 
#define OVERFLOW -2
//Status 是函数的类型,其值是函数结果状态代码
 typedef int Status;
 typedef int ElemType;
 #endif

2.新建XunHLinkList.h

#ifndef XUNHLINKLIST_H
#define XUNHLINKLIST_H
#include "status.h"
typedef struct XLNode
{
	ElemType data;
	struct XLNode *next;
} XLNode,*XunHLinkList;
XunHLinkList XunHuanInitList();
void  XunHuanTraverse(XunHLinkList L);
Status XunHuanIsEmpty(XunHLinkList L);
Status XunHuanLength(XunHLinkList L);
Status XunHuanInsert(XunHLinkList L,int i,int e);
Status XunHuanDelete(XunHLinkList L,int i);
void XunHuanFreeMemory(XunHLinkList *L);
#endif

3.新建XunHLinkList.c

#include "XunHLinkList.h"
#include <stdio.h>
XunHLinkList XunHuanInitList(){
	int i,length=0,date=0;
	XunHLinkList pTail=NULL,p_new=NULL;
    XunHLinkList L=(XunHLinkList)malloc(sizeof(XLNode)) ;
    if(NULL==L){
    	printf("内存分配失败!\n"); 
    }else{
    	L->data=0;
   	    L->next=L;
   	    pTail=L;
    }
    return L;
     
}
void  XunHuanTraverse(XunHLinkList L){
	XunHLinkList pt=L->next;
    printf("链表打印如:");
    while(pt!=L){
    	printf("%d",pt->data);
    	pt=pt->next;
    } 
    printf("\n");
}
Status XunHuanIsEmpty(XunHLinkList L){
	if(L->next==L){
		  return OK;
	}
	
	  else{
  		 return FALSE;
  	}
	 
}
Status XunHuanLength(XunHLinkList L){
	int length=0;
	 XunHLinkList pt=L->next;
	 while(pt!=L){
 		length++;
 		pt=pt->next;
 	}
 	return length;
}
Status XunHuanInsert(XunHLinkList L,int i,int e){
	XunHLinkList p_new=NULL;
	if(i>0&&i<XunHuanLength(L)+2){
	    p_new=(XunHLinkList)malloc(sizeof(XLNode));
	    if(NULL==p_new){
    		printf("内存分配失败!\n");
    		return  ERROR;
    	}
    	while(1){
	    	i--;
	    	if(0==i)
	    	   break;
	    	   L=L->next;
	    }
	    p_new->data=e;
	    p_new->next=L->next;
		L->next=p_new;
		return OK; 
	}
	else  
	   return FALSE;
}
Status XunHuanDelete(XunHLinkList L,int i){
	XunHLinkList pt=NULL;
	if(i>0&&i<XunHuanLength(L)+1){
		while(1){
			i--;
			if(0==i)
			   break;
            L=L->next;
		}
		pt=L->next->next;
		free(L->next);
		L->next=pt;
		return OK;
	}
	else  
	    return FALSE;
}
void XunHuanFreeMemory(XunHLinkList *L){
	XunHLinkList pt=NULL;
	while(*L!=NULL){
		if(*L==(*L)->next) //如果只有头结点 
     	{
		free(*L);
		*L=NULL;
    	}else{
	    	pt=(*L)->next->next;
	    	free((*L)->next);
	    	(*L)->next=pt;
	    }
	}
}

4.新建XunHLinkList_main.c
#include <stdio.h>
#include “XunHLinkList.h”

int main(){
    int flag=0,length=0;
    int position=0,value=0;
    XunHLinkList L=NULL;
    L=XunHuanInitList();
    XunHuanInsert(L,1,1);
	XunHuanInsert(L,2,2);
	XunHuanInsert(L,3,3);
	XunHuanInsert(L,4,4);
	XunHuanInsert(L,5,5);
	XunHuanInsert(L,6,6);
    	flag=XunHuanIsEmpty();
	if(flag){
        printf("单向循环链表为空!\n");
}else{

     length=XunHuanLength(L);
     printf("单向循环链表的长度为:%d\n",length);
     XunHuanTraverse(L);

}
 printf("请输入要插入节点的位置和元素值(空格隔开):");
    scanf("%d %d",&position,&value);
    flag = XunHuanInsert(L, position, value);
	if (flag)
	{
		printf("插入节点成功!\n");
		XunHuanTraverse(L);
	}	
	else
		printf("插入节点失败!\n");
 
	flag = XunHuanIsEmpty(L);
	if (flag)
		printf("单向循环链表为空,不能进行删除操作!\n");
	else
	{
		printf("请输入要删除节点的位置:");
		scanf("%d", &position);
		flag = XunHuanDelete(L, position);
		if (flag)
		{
			printf("删除节点成功!\n");
			XunHuanTraverse(L);
		}	
		else
			printf("删除节点失败!\n");
	}
 
	XunHuanFreeMemory(&L);
	if (NULL == L)
		printf("已成功删除单向循环链表,释放内存完成!\n");
	else
		printf("删除单向循环链表失败,释放内存未完成!\n");
 
	return ERROR;


}

猜你喜欢

转载自blog.csdn.net/qq_43615815/article/details/90140164
今日推荐