1. 程式人生 > >資料結構--雙向非迴圈連結串列

資料結構--雙向非迴圈連結串列

#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;
}

列印不是很好,自己手寫除錯程式碼,歡迎指正和提問。謝謝支援!