栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

线性表链式结构C语言实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线性表链式结构C语言实现

#include "stdio.h"    
#include "string.h"
#include "ctype.h"      
#include "stdlib.h"   

#include "math.h"  
#include "time.h"

typedef struct node
{
	int data;
	struct node* next;
}node;



int get_data(node *L, int i)    //list L  可以看成链表的头指针
{
	int j;
	node* p;
	p = L->next;               //将指针p指向链表第一个节点

	if (i < 1)
	{
		printf("索引错误");
		return 0;
	}

	for (j = 1; j < i; j++)
	{
		if (p->next && p->data)
		{
			p = p->next;
		}
		else
		{
			printf("不存在此结点,超出");
			return 0;
		}
	}

	return p->data;
}

int insert_list(node *L, int i,int value)   //在第i个节点插入数据value
{
	node *s;
	int j;
	if (i < 1)
	{
		printf("索引错误");
		return(0);
	}
	for (j = 1; j < i; j++)
	{
		if (L->next && L->data)
		{
			L = L->next;
		}
		else
		{
			printf("不存在此结点,超出");
			return 0;
		}

	}
	s = (node*)malloc(sizeof(node));
	s->data = value;
	s->next = L->next;
	L->next = s;
	return 1;
}

int insert_list1(node* L, int i, int value[], int size)   //插入多个数据,即数组
{
	node* s;
	int j, n;
	if (i < 1)
	{
		printf("索引错误");
		return(0);
	}
	for (j = 1; j < i; j++)
	{
		if (L->next && L->data)
		{
			L = L->next;
		}
		else
		{
			printf("不存在此结点,超出");
			return 0;
		}

	}
	for (n = 0; n < size; n++)
	{
		s = (node*)malloc(sizeof(node));
		s->data = value[n];
		s->next = L->next;
		L->next = s;
		L = L->next;
	}
	return 1;
}

int delet_list(node* L, int i)
{
	node *q;
	int j;
	for (j = 1; j < i-1; j++)
	{
		if (L->next && L->data)
		{
			L = L->next;
		}
		else
		{
			printf("不存在此结点,超出");
			return 0;
		}

	}
	q = L->next;
	L->next = q->next;
	free(q);
	return 1;
}


int delet_list1(node* L, int i,int n)  //从第i个节点开始,删除n个数字
{
	node* q;
	int j, k;
	for (j = 1; j < i - 1; j++)
	{
		if (L->next && L->data)
		{
			L = L->next;
		}
		else
		{
			printf("不存在此结点,超出");
			return 0;
		}

	}
	for (k = 0; k < n; k++)
	{
		q = L->next;
		L->next = q->next;
		free(q);
	}

	return 1;
}


int getlenght(node *L)
{
	int j = 0;
	while (1)
	{
		if (!L)
		{
			break;
		}

		L = L->next;
		j++;

	}
	j = j - 1;
	return j; //计算长度时不考虑头结点
}

int see_list(node *L)
{
	L = L->next;//头结点没有数据,跳过

	while (1)
	{
		if (!L)
		{
			break;
		}
		printf("%d ", L->data);
		L = L->next;

	}
	printf("n");
	return 1;
}

int clear_list(node *L)
{
	node* q;
	q = L->next;
	while (1)
	{
		if (!q)
		{
			break;
		}
		L->next = q->next;
		free(q);
		q = L->next;
	}
	L->next = NULL;
	return 1;
}



int main()
{
	node L;
	L.next = NULL;
	int i;

	for (i = 0; i < 5; i++)
	{
		insert_list(&L, 1, i);
	}
	see_list(&L);

	insert_list(&L, 5, 99);
	see_list(&L);
	delet_list(&L, 3);
	see_list(&L);
	printf("%dn", getlenght(&L));

	int balance[5] = { 1000, 2, 3, 17, 50 };
	insert_list1(&L, 6,balance,5);
	see_list(&L);
	printf("%dn", getlenght(&L));


	delet_list1(&L, 4,3);
	see_list(&L);
	printf("%dn", getlenght(&L));

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836284.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号