数据结构--双向非循环链表

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;



typedef struct node
{
	int id;
	struct node* prev;
	struct node* next;
}Node,*pNode;

typedef struct list
{
	pNode head;
	pNode tail;
}List,*pList;

pList listInit(void)
{
	pList p = (pList)malloc(sizeof(List));
	p->head = NULL;
	p->tail = NULL;
	return p;
}

// 非循环链表插入头部
int insertList(pList plist,pNode pnode)
{
	if(pnode != NULL)
	{
		if(plist->head != NULL)
		{
			pnode->next = plist->head;
			pnode->prev = NULL;
			plist->head->prev = pnode;
			plist->head = pnode;
		}
		else
		{
			pnode->next = NULL;
			pnode->prev = NULL;
			plist->head = pnode;
			plist->tail = pnode;
		}
		return 0;
	}
	else
	{
		printf("node doesnt exsit\n");
		return -1;
	}
}

// 找到id的节点并返回
pNode seachList(pList list,int id)
{
	pNode tmp = list->head;
	while(tmp && tmp->id != id)
	{
		tmp = tmp->next;
	}
	if(tmp != NULL)
	{
		printf("search id = %d\n",tmp->id);
	}
	else
	{
		printf("cant find id = %d\n",id);
	}
	return tmp;




}


//删除节点时注意释放内存空间
int deleteNode(pList list,int id)
{
	pNode tmp = NULL;
	if(list)
	{
		if( (tmp=seachList(list,id)) != NULL)
		{
			if(tmp == list->head)
			{
				list->head = tmp->next;
				if(tmp == list->tail)//只有一个元素
					list->tail = NULL;
				else
					tmp->next->prev = tmp->prev;
			}
			else if(tmp == list->tail)
			{
				//这里没有即是头也是尾的情况了
				list->tail = tmp->prev;
				tmp->prev->next = NULL;	
			}
			else
			{
				tmp->prev->next = tmp->next;
				tmp->next->prev = tmp->prev;
			}
			printf("delet id = %d\n",tmp->id);
			free(tmp);
			return 0;
		}
		else
		{
			printf("cant find the node = %d\n",id);
			return -1;
		}
	}
	else
	{
		printf("the list is empty cant delete\n");
		return -1;
	}
}

int deletefromhead(pList list)
{
	if(list->head != NULL)
	{
		pNode tmp = list->head;
		list->head = list->head->next;
		if(tmp != list->tail)
			list->head->prev = NULL;
		else //only one node
		{
			list->tail = NULL;
		}
		printf("delete id=%d success\n",tmp->id);
		free(tmp);
		return 0;
	}
	else
	{
		printf("list is empty cant delete form head\n");
		return -1;
	}
}


int destroyList(pList list)
{
	pNode pre = list->head;
	pNode p;
	while(pre!=NULL)
	{
		p = pre->next;
		free(pre);
		pre = p;
	}
	free(list);
	return 0;
}


void printList(const char* s,pList list)
{
	pNode p = list->head;
	printf("\n");
	printf("--------%s-------\n",s);
	if(p == NULL)
		printf("list is empty\n");
	else
	{
		while(p)
		{
			printf("id = %d\n",p->id);
			p = p->next;
		}
	}
}

int main(void)
{
	pNode node1,node2,node3;
	pNode tmp;
	pList mylist = listInit();
	
	node1 = (pNode)malloc(sizeof(Node));
	node2 = (pNode)malloc(sizeof(Node));
	node3 = (pNode)malloc(sizeof(Node));
	node1->id = 1;
	node2->id = 2;
	node3->id = 3;
	/*   释放内存后指针还在注意
	printf("node1 addr = %d\n",node1->id);
	free(node1);
	printf("after free node1 addr = %d\n",node1->id);
	while(1);*/

	//insert to the list  
	insertList(mylist,node1);		
	insertList(mylist,node2);
	insertList(mylist,node3);
	printList("test insert",mylist);//3->2->1

	deletefromhead(mylist);
	printList("test delefromhead",mylist);//2->1

	tmp = seachList(mylist,5);
	tmp = seachList(mylist,2);

	deleteNode(mylist,1);
	printList("deleteNode",mylist);// 2

	deletefromhead(mylist); // null
	printList("deleteNode2",mylist);

	//清理内存
	destroyList(mylist);
	return 0;
}

打印不是很好,自己手写调试代码,欢迎指正和提问。谢谢支持!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值