双向链表

 

link.h

#ifndef _LINK_H
#define _LINK_H

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

#define N 10

typedef int data_type;

//节点
typedef struct node
{
	struct node *per;
	struct node *next;
	data_type data;	
}Node;

//链表
typedef struct link
{
	Node *pHead;
	Node *pTail;
	int count;
}Link;

//设置返回值
enum error
{
	ERROR = -1,
	OK
};

//头尾标志
enum pos
{
	TAIL = -1,
	HEAD
};

//创建空链表
Link *empty_link(void);

//插入节点
int insert_link(Link *pLink,int pos,data_type item);

//删除节点
int del_link(Link *pLink,int pos,data_type *data);

//输出链表
int show_link(Link *pLink);

//销毁链表
int destruction_link(Link **ppLink);



#endif

link.c

#include "../include/link.h"



//创建空链表
Link *empty_link(void)
{
	//创建空链表
	Link *pLink = NULL;
	pLink = (Link *)malloc(sizeof(Link));
	if(NULL == pLink)
	{
		perror("malloc error");
	}
	memset(pLink,'\0',sizeof(Link));
	
	//创建空节点
	pLink->pHead = (Node *)malloc(sizeof(Node));
	if(NULL == pLink)
	{
		perror("malloc error");
		return NULL;
	}
	memset(pLink->pHead,'\0',sizeof(Node));
	
	//count赋初值
	pLink->count = 0;
	
	return pLink;
}

//插入节点
int insert_link(Link *pLink,int pos,data_type item)
{
	//创建新节点
	Node *pNew = NULL;
	pNew = (Node *)malloc(sizeof(Node));
	if(NULL == pNew)
	{
		perror("malloc error");
		return ERROR;
	}
	Node *pTmp = NULL;
	pTmp = pLink->pHead;
	
	switch(pos)
	{
		case HEAD:
			pNew->next = pLink->pHead->next;
			pLink->pHead->next = pNew;
			pNew->per = pLink->pHead;
			pNew->data = item; 
			break;
		case TAIL:

			while(pTmp->next != NULL)
			{		
				pTmp = pTmp->next;
			}
			pTmp->next = pNew;
			pNew->per = pTmp;
			pNew->data = item; 
			break;
		default:
			while(--pos)
			{		
				pTmp = pTmp->next;
			}
			pNew->per = pTmp;
			pNew->next = pTmp->next;
			pTmp->next->per = pNew;
			pTmp->next = pNew;
			
			pNew->data = item; 
			break;
	}
	pLink->count++;
	return OK;
}

//删除节点
int del_link(Link *pLink,int pos,data_type *data)
{
	Node *pTmp = NULL;
	pTmp = pLink->pHead;
	switch(pos)
	{
		case HEAD:
			pTmp = pTmp->next;
			pLink->pHead->next = pTmp->next;
			pTmp->next->per = pLink->pHead;
			*data = pTmp->data;
			free(pTmp);
			pTmp = NULL;	
			break;
		case TAIL:
			while(pTmp->next != NULL)
			{
				pTmp = pTmp->next;
			}
			pTmp->per->next = NULL;
			*data = pTmp->data;
			free(pTmp);
			pTmp = NULL;	
		default:
			while(pos--)
			{
				pTmp = pTmp->next;
			}
			pTmp->per->next = pTmp->next;
			pTmp->next->per = pTmp->per;
			*data = pTmp->data;
			free(pTmp);
			pTmp = NULL;	
			break;
	}
	pLink->count--;
	return OK;
}

//输出链表
int show_link(Link *pLink)
{
	Node *pTmp = NULL;
	pTmp = pLink->pHead;
	while(pTmp->next != NULL)
	{
		pTmp = pTmp->next;
		printf("%d ",pTmp->data);
	}
	printf("\n");
	return OK;
}

//销毁链表
int destruction_link(Link **ppLink)
{
	Node *pTmp = NULL;
	while((*ppLink)->pHead->next != NULL)
	{
		pTmp = (*ppLink)->pHead->next;
		(*ppLink)->pHead->next = pTmp->next;
		free(pTmp);
		pTmp = NULL;
	}
	free((*ppLink)->pHead);
	(*ppLink)->pHead = NULL;
	free(*ppLink);
	*ppLink = NULL;
	return OK;
}

main:

#include "../include/link.h"


int menu(void)
{
	int op = 0;

	printf("1----------insert_link\n");
	printf("2----------del_link\n");
	printf("3----------show_link\n");
	printf("-1----------exit\n");
	scanf("%d",&op);
	return op;
}


int main(void)
{
	Link *p = NULL;
	p = empty_link();
	//printf("OK\n");
	
	int pos = 0;
	int op = 0;
	data_type item = 0;
	int ret = 0;
	
	system("clear"); //在C中调用Linux命令
	
	while(1)
	{
		op = menu();
		if(-1 == op)
		break;	
		switch(op)	
		{
			case 1:
				//insert
				printf("请输入您要插入的位置和数据\n");
				scanf("%d %d",&pos,&item);
				ret = insert_link(p,pos,item);
				if(ret != OK)
				{
					printf("插入失败\n");
					if(ret == ERROR)
					{
						printf("插入位置有误\n");
					}
				}
				else
				{
					printf("插入成功\n");
				}
				break;	
			
			case 2:
				//delete
				printf("请输入您要删除位置\n");
				scanf("%d",&pos);
				ret = del_link(p,pos,&item);
				if(ret==OK)
				{
					printf("删除成功,数据为%d\n",item);	
				}			
				break;
			case 3:
				//show	
				show_link(p);
				break;


			default:
				printf("error");
		}	
	}

	destruction_link(&p);
	return 0;	
}

 

posted @ 2022-05-25 14:32  西北小蚂蚁  阅读(16)  评论(0编辑  收藏  举报