链表 线性表 的 头插法 尾插法 查找 删除 逆序 排序

头文件 

linklist.h

#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef int data_t;
typedef struct node
{
	data_t data;	//数据域
	struct node *next;//指针域
}node_t,*linklist_p;

node_t *linklist_create();
node_t *linklist_create_tail();
data_t linklist_data_search(node_t *head,int offset);

int  linklist_length(node_t *head);
int  linklist_data_insert(node_t *head,int offset ,data_t data);
int  linklist_offset_search(node_t *head,data_t data);
int  linklist_offset_deleat(node_t *head);

void linklist_show(node_t *head);
void linklist_reverse(node_t *head);
void linklist_maopo(node_t *head);

#endif

函数文件

linklist.c

#include<stdio.h>
#include<string.h>
#include <strings.h>
#include<stdlib.h>
#include"linklist.h"
/**********************************************/
//function:头插法创建线性表
/**********************************************/

node_t *linklist_create()
{
	node_t *head = (node_t *)malloc(sizeof(node_t));
	if(head == NULL)
	{
		puts("malloc is fial");
		return NULL;
	}
	head->next = NULL;
	head->data =-1;
	int data;
	putchar('\n');
	printf("input data :(-1)=end\n");
	while(1)
	{
		scanf("%d",&data);
		if(-1 == data)
		{
			break;
		}
		else
		{
			node_t *p = (node_t *)malloc(sizeof(node_t));
			p->data = data;
			p->next = head->next;
			head->next=p;
		}
	}
	return head;
}

/**********************************************/
//function:尾插法创建线性表
/**********************************************/

node_t *linklist_create_tail()
{
	node_t *head = (node_t *)malloc(sizeof(node_t));
	if(head == NULL)
	{
		puts("malloc is fial");
		return NULL;
	}
	head->next = NULL;
	head->data =-1;
	node_t *temp = head;
	int data;
	putchar('\n');
	printf("input data :(-1)=end\n");
	while(1)
	{
		scanf("%d",&data);
		if(-1 == data)
		{
			break;
		}
		else
		{
			node_t *p = (node_t *)malloc(sizeof(node_t));
			p->data = data;
			p->next = temp->next;
			temp->next = p;
			temp = p;
		}
	}
	return head;
}

/**********************************************/
//function:线性表的显示
/**********************************************/

void linklist_show(node_t *head)
{
	node_t *p = head->next;
	while(NULL != p)
	{
		printf("  %4d",p->data);
		p=p->next;
	}	

}


/**********************************************/
//function:在线性表的任意位置插入数据
/**********************************************/
int linklist_data_insert(node_t *head,int offset ,data_t data)
{
	//判断位置是否有效
	if((offset < 0)||(offset >linklist_length(head)))
	{
		puts("input offset is error");
		return -1;
	}
	//查找要插入位置的上一个节点
	int i=0;
	node_t *p = head;
	for(i=0;i<offset;i++)
	{
		p=p->next;
	}
	//封装新节点
	node_t *q = (node_t *)malloc(sizeof(node_t));
	q->data = data;//存入数据
	//节点插入
	q->next = p->next;
	p->next =q;
 return 0;
}

/**********************************************/
//function:求线性表的长度
/**********************************************/
int linklist_length(node_t *head)
{
	node_t *l=head->next;
	int length = 0;
	while(l != NULL)
	{
		l=l->next;
		length++;
	}
	return length;
}


/**********************************************/
//function:线性表中按值查找位置
/**********************************************/
int linklist_offset_search(node_t *head,data_t data)
{
	node_t *p = head->next;
	int offset = 0;
	if (p == NULL)
	{
		puts("this linklist is empty");
		return -1;
	}
	else
	{
		while((p->next != NULL)&&(p->data != data))
		{
			offset++;
			p=p->next;
		}
	}
	return offset;
}

/**********************************************/
//function:线性表中按位置查找值
/**********************************************/
data_t linklist_data_search(node_t *head,int offset)
{
	
	//判断位置是否有效
	if((offset < 0)||(offset >linklist_length(head)))
	{
		puts("input offset is error");
		return -1;
	}
	else
	{
		node_t *p = head->next;
		int i=0;
		while(i<offset)
		{
			p=p->next;
			i++;
		}
	return p->data;
	}
}

/**********************************************/
//function:线性表中元素的删除
/**********************************************/
int linklist_offset_deleat(node_t *head)
{
	node_t *t = head;
	node_t *l,*r;
	putchar('\n');
	puts("input you need deleat number offset");
	int offset ,i;
	scanf("%d",&offset);
	for(i=0;i<offset;i++)
	{
		l=t->next; //保存好待删除的指针域的指向
		r=t;
		t=l;
	}
	l=t->next;
	r->next = l;
	free(t);
	return 0;
}
/**********************************************/
//function:线性表中元素的逆序
/**********************************************/
void linklist_reverse(node_t *head)
{
	node_t *h=head->next;
	int length=linklist_length(head);
	head->next =NULL;
	//把头的指针域保存下来 然后断开用头插法插入数据
	node_t *t;
	while(length--)
	{
		t=h->next;
		h->next =head->next;
		head->next =h;
		h=t;
	}
	return ;
}
/**********************************************/
//function:线性表中的数据排序
//有bug没有直接运用结构实现用数组转存了
/**********************************************/
void linklist_maopo(node_t *head)
{
	 if(NULL == head)
        {
                puts("this linklist is null");
                return ;
        }
	int i,j,length;
	int data[64]={0};
	node_t *p=head->next;
	length = linklist_length(head);
	for(i=0;i<length;i++)
	{
		data[i]=p->data;
		p=p->next;
	}
	for(i=0;i<length-1;i++)
	  for(j=0;j<length-i-1;j++)
		{
			if(data[j]<data[j+1])
			{
				data[j]=data[j]^data[j+1];
				data[j+1]=data[j]^data[j+1];
				data[j]=data[j]^data[j+1];
			}
		}
	
	for(i=0;i<length;i++)
	{
		printf("  %4d",data[i]);
	}
	putchar('\n');
	return ;
}



















主函数

main.c

#include<stdio.h>
#include<string.h>
#include <strings.h>
#include<stdlib.h>
#include"linklist.h"
int main()
{
 	puts("头插法插入数据");
	node_t *head =linklist_create();
 	if(NULL == head)
	{
		puts("create fail");
		return -1;
	}
	linklist_show(head);
	/*
	int set = linklist_data_search(head,1);
	printf("1 offset =%d\n",set);
	
	data_t d = linklist_offset_search(head,1);
	printf("1 data =%d\n",d);
	*/
	putchar('\n');
	puts("尾插法插入数据");
 	node_t *tail =linklist_create_tail();
 	if(NULL == tail)
	{
		puts("create fail");
		return -1;
	}
	linklist_show(tail);
	putchar('\n');
	puts("按位置插入数据,这里设置位置从0开始");
	linklist_data_insert(head,0,666);
	linklist_show(head);
	linklist_offset_deleat(head);
	linklist_show(head);
	linklist_reverse(head);
	putchar('\n');
	puts("逆序后的结果");
	linklist_show(head);
	putchar('\n');
	linklist_maopo(head);	
	putchar('\n');
 return 0;
}

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值