数据结构-链表

1.head.h

#ifndef __HEAD_H_
#define __HEAD_H_
#include 
#include 
#include 
typedef int type;
enum A{SUCCESS,FLASE=-1};
typedef struct Node
{
	union
	{
		type data;
		type len;
	};
	struct Node *next;
}*Llist;
Llist create(type flag);
int insert_head(Llist head,type element);
int output(Llist head);
int insert_rear(Llist head,type element);
int delete_head(Llist head);
int delete_rear(Llist head);
int delete_sub(Llist head,type sub);
int update_sub(Llist head,type sub,type element);
int insert_sub(Llist head,type sub,type element);
int search_sub(Llist head,type sub);
int search_element(Llist head,type element);
int update_element(Llist head,type element,type element0);
int delete_element(Llist head,type element);
int reverse(Llist head);
#endif

2.test.c

#include "head.h"
Llist create(type flag)
{
	Llist list=(Llist)malloc(sizeof(struct Node));
	if(flag==1)
	{
	list->len=0;
	}
	else if(flag==0)
	{
	list->data=0;
	}
	list->next=NULL;
	return list;
}

int insert_head(Llist head,type element)
{
	if(head==NULL)
	{
		return FLASE;
	}
	Llist p=create(0);
	p->data=element;
	p->next=head->next;
	head->next=p;
	head->len++;
	return SUCCESS;
}

int output(Llist head)
{
	if(head==NULL)
	{
		return FLASE;
	}
	Llist p=head->next;
	printf("链表输出为:");
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
return SUCCESS;
}

int insert_rear(Llist head,type element)
{
	if(head==NULL)
		return FLASE;
	Llist p=head;
	while(p->next!=NULL)
		p=p->next;
	Llist s=create(0);
	s->data=element;
	p->next=s;
	head->len++;
	return FLASE;
}

int delete_head(Llist head)
{
	if(head==NULL)
		return FLASE;
	if(head->next==NULL)
		return FLASE;
	Llist p=head->next;
	head->next=p->next;
	free(p);
	p=NULL;
	head->len--;
	return SUCCESS;
}

int delete_rear(Llist head)
{
	if(head==NULL)
		return FLASE;
	if(head->next==NULL)
		return FLASE;
	Llist p=head;
	while(p->next->next!=NULL)
		p=p->next;
	Llist s=p->next;
	p->next=NULL;
	free(s);
	s=NULL;
	head->len--;
	return FLASE;
}

int insert_sub(Llist head,type sub,type element)
{
	if(head==NULL)
		return FLASE;
	Llist p=head;
	if(sub<=0||sub>head->len)
		return FLASE;
	if(sub==1)
	{
	insert_head(head,element);
	}
	else{
	for(type i=0;inext;
	}
	Llist s=create(0);
	s->data=element;
	s->next=p->next;
	p->next=s;
	head->len++;
	}
return SUCCESS;
}

int delete_sub(Llist head,type sub)
{
	if(head==NULL)
		return FLASE;
	Llist p=head;
	if(sub<=0||sub>head->len)
		return FLASE;
	if(sub==head->len)
	{
		delete_rear(head);
	}
	else
	{
		for(type i=0;inext;
		}
		Llist del=p->next;
		p->next=del->next;
		free(del);
		del=NULL;
	}
	head->len--;
	return SUCCESS;
}

int update_sub(Llist head,type sub,type element)
{
	if(head==NULL)
		return FLASE;
	Llist p=head;
	if(sub<=0||sub>head->len)
		return FLASE;
	for(type i=0;inext;
	}
	p->data=element;
	return SUCCESS;
}

int search_sub(Llist head,type sub)
{
	if(head==NULL)
		return FLASE;
	Llist p=head;
	if(sub<=0||sub>head->len)
		return FLASE;
	for(type i=0;inext;
	}
	printf("在第%d位的值是:%d\n",sub,p->data);
return SUCCESS;
}

int search_element(Llist head,type element)
{
	if(head==NULL)
		return FLASE;
	Llist p=head;
	type count=0;
	for(type i=0;ilen;i++)
	{
		p=p->next;
		count++;
		if(p->data==element)
		{
			printf("数据%d在第%d位\n",element,count);
		}
	}
	return SUCCESS;
}

int update_element(Llist head,type element,type element0)
{
	if(head==NULL)
		return FLASE;
	Llist p=head;
	for(type i=0;ilen;i++)
	{
		p=p->next;
		if(p->data==element)
		{
			p->data=element0;
		}
	}
return SUCCESS;
}

int delete_element(Llist head,type element)
{
	if(head==NULL)
		return FLASE;
	Llist p=head;
	for(type i=0;ilen;i++)
	{
		if(p->next->data==element)
		{
		Llist s=p->next;
		p->next=s->next;
		free(s);
		s=NULL;
		head->len--;
		}
		else
		{
		p=p->next;
		}
	}
return SUCCESS;
}

int reverse(Llist head)
{
	if(head==NULL||head->next==NULL)
		return FLASE;
	Llist p=head->next;
	head->next=NULL;
	for(type i=0;ilen;i++)
	{
		Llist s=p;
		p=p->next;
		s->next=head->next;
		head->next=s;
	}
	 p=head->next;
	printf("链表逆置为:");
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
return SUCCESS;
}

3.main.c

#include "head.h"
int main(int argc,const char *argv[])
{
	Llist head=create(1);
	type n,element,sub,element0;
	printf("输入头插次数:");
	scanf("%d",&n);
	for(type i=0;i

你可能感兴趣的:(数据结构-链表)