数据结构之链表(c)

#include <stdio.h>
#include <stdlib.h>
/************************************************************************/
/* 以下为C语言实现单链表,双链表,循环链表                                                       
/************************************************************************/


//单链表定义
typedef char Elemplment;
#define  LEN sizeof(Lnode)
typedef struct Node
{
	Elemplment data;
	struct Node* next;
}Lnode;

// 带头节点的创建在头部插入                                                         
Lnode* creatHeader()
{
	Lnode *p,*head;
	head = (Lnode *)malloc(LEN);
	head->next = NULL;
	while(1)
	{
		p = (Lnode *)malloc(LEN);
		scanf("%c",&p->data);
		p->next = head->next;
		head->next = p;
		printf("");
		if (getchar()=='n')
		{
			break;
		}
	}
	return head;
}

// 带头节点的在尾部插入                                                         
Lnode* creatTail()
{
	Lnode *p,*tail,*head;
	tail = head =(Lnode*)malloc(LEN);
	head->next = NULL;
	while (1)
	{
		p = (Lnode *)malloc(LEN);
		scanf("%c",&p->data);
		tail->next = p;
		tail = p;
		p->next = NULL;
		printf("");
		if (getchar() == 'n')
		{
			break;
		}		
	}
	return head;
}

// 得到长度                                                         
int getLenth(Lnode *head)
{
	int j= 0;
	Lnode *p = head;
	while(p!=NULL)
	{
		j++;
		p= p->next;
	}
	return j;
}

// 在一个位置插入 一个元素                                                       
int insertLocation(Lnode*head,int i,Elemplment x)
{
	int j=1;
	Lnode *p,*s;
	p=head->next;
	while(p!=NULL&& j<i)
	{
		j++;
		p = p->next;
	}
	if(j==i)
	{
		s = (Lnode *)malloc(LEN);
		s->data = x;
		s->next = p->next;
		p->next = s;
		return 1;
	}
	else
		return 0;
}

//通过位置删除一个元素                                                        
int deleteLocation(Lnode* head,int i)
{
	Lnode *p,*q;
	int j=1;
	p = head->next;
	while(p!=NULL&& j<i-1)
	{
		p++;
		p = p->next;
	}
	if (j<i-1)
	{
		return 0;
	}
	else if (p->next =NULL)
	{
		return 0;
	}
	q = p->next;
	p->next = q->next;
	free(q);
	return 1;
}

// 查找元素返回节点指针 用数据                                                   
Lnode * Locate(Lnode* head,Elemplment x)
{
	Lnode* p;
	p = head;
	while (p!=NULL&&p->data ==x)
	{
		p = p->next;
	}
	return p;
}

//查找元素返回节点指针 用位置                                                   
Lnode * LocateWithLocation(Lnode* head,int i)
{
	int j = 1;
	Lnode *p;
	p = head->next;
	while(p!=NULL&& j<i)
	{
		j++;
		p = p->next;
	}
	if (i==j)
	{
		return p;
	}
	else 
		return NULL;
}

// 单循环链表初始化空表        
Lnode* initCirculate(Lnode *head)
{
	head= (Lnode *)malloc(LEN);
	head->next = head;
	return head;
}

// 单链表初始化空表 
int isEmpty(Lnode *head)
{
	if (head->next = head)
	{
		return 0;
	}
	else
		return 1;
}

//遍历单链表
void view (Lnode* head)
{
	Lnode *p;
	p = head->next;
	while(p!=head)
	{
		putchar(p->data);
		p = p->next;
	}

}

//循环双链表定义
#define  LENDOUBLE sizeof(DouCirList)
typedef struct DouCirList
{
	Elemplment data;
	DouCirList *next,*prior;
}LDouCirList;

//初始化一个空双循环链表
LDouCirList* initLDouCirList(LDouCirList* head)
{
	head = (LDouCirList*)malloc(LENDOUBLE);
	head->next = head;
	head->prior =head;
	return head;
}

//双循环链表建立
LDouCirList* initLDouCirListWithData(LDouCirList* head)
{
	LDouCirList *p;
	head = (LDouCirList*)malloc(LENDOUBLE);
	head->next =head;
	head->prior =head;
	while(1)
	{
		p =(LDouCirList*)malloc(LENDOUBLE);
		scanf("%c",&p->data);
		p->next=head->next;
		head->next= p;
		p->prior =head;
		p->next->prior = p;
		printf("");
		if (getchar()=='n')
		{
			break;
		}	
	}
	return head;
}

void main ()
{
	printf("hello world");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值