设为首页 加入收藏

TOP

C语言线性单链表相关函数和算法的基本实现详细教程(一)
2018-05-28 09:03:26 】 浏览:499
Tags:语言 线性 单链表 相关 函数 算法 基本 实现 详细 教程

备考期间尝试写了一些基本数据结构的C语言实现,现做以下记录(基本数据元以int型为例):

全局定义及依赖:

#include 
  
    
#include 
   
     #define OK 1 #define ERROR 0 #define END NULL
   
  

链表结点定义:

typedef struct Lnode
{
	int data;
	struct Lnode *next;
}Node,*linkedList;

链表初始化:

//初始化链表 
linkedList initList()
{
	Node *L;
	L = (Node *)malloc(sizeof(Node));
	if(L==NULL)
	{
		printf("Init error!");
	}
	L->next = NULL;
	printf("Init OK!\n");
	return L;
}

头插法生成链表 即在头结点后插入新元素 :

//头插法生成链表 即在头结点后插入新元素 
linkedList createListH(int n)
{
	int i = 0;
	Node *L;
	L = (Node *)malloc(sizeof(Node));
	L->next = NULL;
	
	int x;
	while(i
  
   data = x;
		p->next = L->next;
		L->next = p;
		i++;
	}
	return L;
}
  

尾插法生成链表 即在尾结点后插入新元素:

//尾插法生成链表 即在尾结点后插入新元素
linkedList createListT(int n)
{
	int i = 0;
	Node *T;
	T = (Node *)malloc(sizeof(Node));
	T->next = NULL;
	
	Node *r;
	r = T;
	int x;
	while(i
  
   data = x;
		r->next = p;
		r = p;
		i++;
	}
	r->next = NULL;
	return T;
}
  

计算链表长度:

//calculate the length
int calculateLength(linkedList L)
{
	int length = 0;
	Node *p = L;
	while(p->next)
	{
		length++;
		p = p->next;
	}
	return length;
} 

在指定链表的指定索引处插入新值:

//insert element into index x
int insertElementIntoX(linkedList L,int index,int e)
{
	Node *p = L;
	int length = calculateLength(L);

	if(index<1||index>length+1){return ERROR;}
	else
	{
		for(int i = 1;i
  
   next;
		}
		Node *q = (Node *)malloc(sizeof(Node));
		q->data = e;
		q->next = p->next;
		p->next = q;
		return OK;
	}
}
  

通过索引删除指定链表中的元素:

//delete element by index
int deleteElementByIndex(linkedList L,int index)
{
	int length = calculateLength(L);
	if(index<1||index>length){return ERROR;}
	else
	{
		Node *p = L;
		for(int i = 1;i
  
   next;
		}
		Node *q = p->next;
		p->next = q->next;
		free(q);
		return OK;
	}
}
  

遍历并打印链表中所有结点元素:

//遍历 
void traversalList(linkedList L)
{
	Node *p = L->next;
	while(p!=END)
	{
		printf("%d ",p->data);
		p = p->next;
	}
	printf("\n");
}

对两个链表做非递减归并:

linkedList mergeDecline(linkedList La,linkedList Lb)
{
	Node *a = La->next;
	Node *b = Lb->next;
	free(La);
	free(Lb);
	linkedList Lc = initList();
	Node *temp = Lc;
	while(a!=NULL&&b!=NULL)
	{
		if(a->data<=b->data)
		{
			temp->next = a;
			a = a->next;
			temp = temp->next; 
		}
		else
		{
			temp->next = b;
			b = b->next;
			temp = temp->next;
		}
	}
	temp->next == NULL;
	if(a!=NULL){temp->next = a;}
	if(b!=NULL){temp->next = b;}
	return Lc;
}

删除给定链表中所有重复元素(对重复元素只保留其中一个):

//delete repeating node  
void deleteRepeatingNode(linkedList L)
{
	Node *p,*q,*r;
	p = L->next;
	if(p==NULL){return;}
	while(p->next)
	{
		q = p;
		while(q->next)
		{
			if(q->next->data==p->data)
			{
				r = q->next;
				q->next=r->next;
				free(r);
			}
			else
			{
				q = q->next;
			}
		}
		p = p->next;
	}
}

将链表中的元素按从小到大进行排序:

//sort from small to large
int sortFSL(linkedList L)
{
	if(L->next==NULL){return ERROR;}
	else
	{
		Node *r = L;
		Node *p = r->next;
		Node *q =
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇异常和Bug、异常和BUG的对比和C语.. 下一篇冒泡排序算法C语言代码测试总结

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目