五月计划 --- 数据结构day5

双链表:

double_link.h

     1	#ifndef __DOUBLE_LINK__
     2	#define __DOUBLE_LINK__
     3	#include<stdio.h>
     4	#include<stdlib.h>
     5	#include<string.h>
     6	typedef int datatype;
     7	
     8	typedef struct Node
     9	{
    10		struct Node *prio;
    11		union
    12		{
    13			datatype data;
    14			int len;
    15		};
    16		struct Node *next;
    17	}*dlnode,dlist;
    18	
    19	//创建双向链表
    20	dlist *list_create();
    21	//判空
    22	int list_empty(dlist * dl);
    23	//申请结点封装数据
    24	dlnode node_buy(dlist *dl , datatype data);
    25	//头插
    26	int list_insert_head(dlist *l,datatype data);
    27	//便利
    28	void list_show(dlist *l);
    29	//任意位置插入
    30	int list_insert_pos(dlist *l,int pos,datatype data);
    31	//任意位置删除
    32	int list_delete_pos(dlist *l,int pos);
    33	//按位置查找指定的结点
    34	dlnode list_sreach_pos(dlist *l,int pos);
    35	//释放链表
    36	void list_free(dlist *l);
    37	#endif

double_link_fun.c

     1	#include"double_link.h"
     2	
     3	//创建双向链表
     4	dlist *list_create()
     5	{
     6		dlist *dl = (dlist *)malloc(sizeof(dlist));
     7		if (!dl)
     8		{
     9			printf("创建失败!\n");
    10			return NULL;
    11		}
    12		dl->next = NULL;
    13		dl->prio = NULL;
    14		dl->len = 0;
    15	
    16		printf("创建成功!\n");
    17		return dl;
    18	}
    19	//判空
    20	int list_empty(dlist * dl)
    21	{
    22		if (!dl)
    23		{
    24			printf("未建表!\n");
    25			return -1;
    26		}
    27		if (dl->next)
    28			return 0;
    29		return 1;
    30	}
    31	//申请结点封装数据
    32	dlnode node_buy(dlist *dl , datatype data)
    33	{
    34		dlnode node = (dlnode)malloc(sizeof(dlist));
    35		if (!node)
    36		{
    37			printf("创建失败!\n");
    38			return NULL;
    39		}
    40		node->data = data;
    41		node->next = NULL;
    42		node->prio = NULL;
    43		return node;
    44	}
    45	//头插
    46	int list_insert_head(dlist *dl,datatype data)
    47	{	
    48		dlnode node = node_buy(dl , data);
    49		if (list_empty(dl)==-1)
    50			return 0;
    51		else if (list_empty(dl)==1)
    52		{
    53			node->prio = dl; 
    54			dl->next = node;
    55			dl->len++;
    56		}
    57		else
    58		{
    59			node->next = dl->next;
    60			node->prio = node->next->prio;
    61			node->next->prio = node;
    62			dl->next = node;
    63			dl->len++;
    64	
    65		}
    66		printf("添加成功!\n");
    67		return 1;
    68		
    69	}
    70	//便利
    71	void list_show(dlist *dl)
    72	{
    73		if (list_empty(dl))
    74			return;
    75		int i = 0;
    76		while (dl=dl->next)
    77		{
    78			i++;
    79			printf("第%d个结点元素是:%d\n",i,dl->data);
    80		}
    81		printf("便利完成!\n");
    82	}
    83	//任意位置插入
    84	int list_insert_pos(dlist *dl,int pos,datatype data)
    85	{
    86		dlnode node = node_buy(dl , data);
    87		if (list_empty(dl)==-1)
    88			return 0;
    89		else if (list_empty(dl) == 1)
    90		{
    91				node->prio = dl; 
    92				dl->next = node;	
    93				dl->len++;
    94		}
    95		else if (list_empty(dl) == 0)
    96		{
    97			int i;
    98			dlnode ln = dl;
    99			for (i=0;i<pos-1;i++) 
   100				ln=ln->next;
   101			printf("%d\n",dl->len);
   102			if (pos-1 == dl->len)
   103			{
   104				node->prio = ln; 
   105				ln->next = node;
   106				dl->len++;
   107			}
   108			else if (pos > 0 && pos < dl->len+1)
   109			{
   110				dl->len++;
   111				node->next = ln->next;
   112				node->prio = node->next->prio;
   113				node->next->prio = node;
   114				ln->next = node;
   115			}
   116			else
   117			{
   118				printf("位置错误!\n");
   119				return 0;
   120			}
   121			
   122		}
   123		printf("%d\n",dl->len);
   124		printf("添加成功!\n");
   125		return 1;
   126	
   127	}
   128	//任意位置删除
   129	int list_delete_pos(dlist *dl,int pos)
   130	{
   131		if (list_empty(dl))
   132			return 0;
   133		else if (list_empty(dl) == 0)
   134		{
   135			dlnode node = dl;
   136			dlnode p;
   137			int i;
   138			for (i=0;i<pos;i++)
   139				node = node->next;
   140			if (pos == dl->len)
   141			{
   142				p=node;
   143				node = node->prio;
   144				free(p);
   145				node->next = NULL;
   146				dl->len--;
   147			}
   148			else if(pos >= 1 && pos < dl->len)
   149			{
   150				p=node;
   151				node->prio->next = node->next;
   152				node->next->prio = node->prio;
   153				free(p);
   154				dl->len--;
   155			}
   156			else
   157			{
   158				printf("位置错误!\n");
   159				return 0;
   160			}
   161		}
   162		printf("删除成功!\n");
   163		return 1;
   164	}
   165	//按位置查找指定的结点
   166	dlnode list_sreach_pos(dlist *dl,int pos)
   167	{
   168		if (list_empty(dl))
   169			return 0;
   170		int i;
   171		dlnode node = dl;
   172		for (i=0;i<pos;i++)
   173			node = node->next;
   174		return node;
   175	}
   176	//释放链表
   177	void list_free(dlist *dl)
   178	{
   179		if (list_empty(dl) == -1)
   180			return;
   181		dlnode node = dl;
   182		while (node->next)
   183			node = node->next;
   184		dlnode p = node;
   185		while (node)
   186		{
   187			node = node->prio;
   188			free(p);
   189			p=node;
   190		}
   191		dl = NULL;
   192		printf("表已释放!\n");
   193	}

double_link_main.c

     1	#include"double_link.h"
     2	
     3	int main(int argc, const char *argv[])
     4	{
     5		dlist *dl = list_create();
     6		if (!dl)
     7		{
     8			printf("创建失败!\n");
     9			return -1;
    10		}
    11	
    12		list_insert_pos(dl,1,9);
    13		list_insert_head(dl,1);
    14		list_insert_head(dl,2);
    15		list_insert_head(dl,3);
    16		list_insert_head(dl,4);
    17		list_show(dl);
    18	
    19		list_insert_pos(dl,1,8);
    20		list_insert_pos(dl,2,7);
    21		list_show(dl);
    22		list_insert_pos(dl,8,6);
    23		list_show(dl);
    24	
    25		list_delete_pos(dl,2);
    26		list_delete_pos(dl,dl->len);
    27		list_show(dl);
    28		
    29		dlnode node = list_sreach_pos(dl,1);
    30		printf("找到结点的元素是:%d\n",node->data);
    31		
    32		list_free(dl);
    33	
    34		return 0;
    35	}
220	//按位置进行修改
   221	int list_change_pos(list *l,int pos,stu s)
   222	{
   223		lnode node = list_search_pos(l,pos);
   224		if (!node)
   225		{
   226			printf("没查到该结点\n");
   227			return 0;
   228		}
   229		node->data = s;
   230		return 1;
   231	}
 232	//按值进行修改
   233	int list_change_pos(list *l,char *name,stu s)
   234	{
   235		lnode node = list_search_value(l,name);
   236		if (!node)
   237		{
   238			printf("没查到该结点\n");
   239			return 0;
   240		}
   241		node->data = s;
   242		return 1;
   243	}
  244	//链表排序
   245	void sort_list(list *l,int flag)
   246	{
   247		if (list_empty || !l)
   248		{
   249			printf("表空或损坏!\n");
   250			return 0;
   251		}
   252		lnode node_r = l->next;
   253		lnode node_l = l->next;
   254		lnode node_max = l->next;
   255		while (node_r->next!=NULL)
   256		{
   257			node_l = node_r;
   258			while (node_l->next != NULL)
   259			{
   260				node_l= node_l->next;
   261				if (node_max->data.sco < node_l->data.sco)
   262					node_max = node_l;
   263			}
   264			if (node_max != node_r)
   265			{
   266				node_l = node_max;
   267				node_l.data = node_r.data;
   268				node_r.data = node_max.data;
   269			}
   270			node_r=node_r->next;
   271		}
   272	
   273	}
 303	//递归反转
   304	lnode list_reverse_rec(list *l)
   305	{
   306		lnode node = l->next;
   307		if (node->next == NULL)
   308			return node;
   309		else
   310			l->next = l->next->next;
   311		lnode p = list_reverse_rec(l);
   312		p->next = node;
   313		node->next = NULL;
   314		return node;
   315	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值