线性表 - 链表

线性表 - 链表

将线性表L=(a0,a1,……,an-1)中各元素分布在存储器的不同存储块,
称为结点,通过地址或指针建立它们之间的联系,所得到的存储结构
为链表结构

// 单链表结构体类型:
typedef int datatype; // int 可扩展为结构体、联合体、float/double等数据类型

typedef struct node
{
	datatype data;			// 节点:数据域
	struct node * next; 	// 节点:指针域(指向后继元素的地址)
} linklist, *linklist_p;	// linklist 为链表的结构体;linklist_p为指向链表的指针类型
/*================================================================
 *   文件名称:linklist.c
 *   创 建 者:
 *   创建日期:
 *   描    述:
 *
 ================================================================*/


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

typedef int datatype;

typedef struct node
{
	datatype data;
	struct node * next;
} linklist, *linklist_p;

linklist_p list_create();
int head_insert(linklist_p L,datatype value);
void list_show(linklist_p L);
int head_delete(linklist_p L);
int list_empty(linklist_p L);
int order_insert(linklist_p L,datatype value);
int value_delete(linklist_p L,datatype value);
void list_change(linklist_p L,datatype old,datatype new);
int list_find(linklist_p L,datatype value);
int list_reverse(linklist_p L);


int main(int argc, char *argv[])
{
	linklist_p L =NULL;
	if((L = list_create())==NULL)
	{
		printf("create to list_create \n");
		return -1;
	}

	/*	head_insert(L,1);
		head_insert(L,2);
		head_insert(L,3);
		head_insert(L,4);
		head_insert(L,5);
		list_show(L);
		head_delete(L);
		list_show(L);
		*/
	order_insert(L,12);
	order_insert(L,34);
	order_insert(L,2);
	order_insert(L,5);
	order_insert(L,9);
	list_show(L);
	value_delete(L,2222);
	list_change(L,2,567);
	list_show(L);
//	list_find(L,5);
//	list_find(L,9);

	list_reverse(L);
	list_show(L);
	return 0;
}



linklist_p list_create()
{
	linklist_p L =NULL;
	L  = (linklist_p)malloc(sizeof(linklist));
	if(NULL==L)
	{
		printf("fail to malloc \n");
		return NULL;
	}
	L->next = NULL;
	return L;
}

int head_insert(linklist_p L,datatype value)
{
	linklist_p p = NULL;
	if(( p = (linklist_p)malloc(sizeof(linklist)))==NULL)
	{
		printf("fail to malloc\n");
		return -1;
	}
	p->data = value;
	p->next = L->next;
	L->next = p;
	return 0;
}


void list_show(linklist_p L)
{
#if 0
	linklist_p p = L;
	while(p->next != NULL)
	{
		printf("%d ",p->data);
		p = p->next;
	}
	printf("%d",p->data);
	putchar(10);
#else
	linklist_p p = L;
	while(p->next != NULL)
	{
		printf("%d ",p->next->data);
		p = p->next;
	}
	putchar(10);

#endif
}


int head_delete(linklist_p L)
{
	linklist_p p = L->next;
	if(list_empty(L))
	{
		printf("empty \n");
		return -1;
	}
	L->next = p->next;
	free(p);
	p=NULL;
	return 0;
}

//empty ---1  
int list_empty(linklist_p L)
{
	return L->next == NULL ? 1 : 0 ;
}

int order_insert(linklist_p L,datatype value)
{
	linklist_p p =NULL, q=L;
	while(q->next != NULL && q->next->data <value)
	{
		q = q->next;
	}
	if((p=(linklist_p)malloc(sizeof(linklist))) == NULL)
	{
		printf("fail to malloc \n");
		return -1;
	}
	p->data = value;
	p->next = q->next;
	q->next = p;


	return 0;
}

int value_delete(linklist_p L,datatype value)
{
	linklist_p p=L,q=NULL;
	while(p->next != NULL && p->next->data != value)
		p = p->next;
	if(p->next == NULL)
	{
		printf("no find %d\n",value);
		return -1;
	}
	q = p->next;
	p->next = q->next;
	free(q);
	q=NULL;
	return 0;
}

void list_change(linklist_p L,datatype old,datatype new)
{
	while(L->next != NULL)
	{
		if(L->next->data == old)
		{
			L->next->data = new;
		}
		L = L->next;
	}
}

int list_find(linklist_p L,datatype value)
{
	int i=0;
	while(L->next != NULL)
	{
		if(L->next->data == value)
		{
			printf("find %d in%d\n",value,i);
			return 1;
		}
		L = L->next;
		i++;
	}
	return 0;
}


int list_reverse(linklist_p L)
{
	linklist_p p=L->next , q=NULL;
	L->next = NULL;

	while( p != NULL)
	{
		q = p;
		p = p->next;
		q->next = L->next ;
		L->next = q;
	}


}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值