数据结构之链表实现

抽象数据类型(ADT)表有顺序表和链表两种,顺序表可以通过使用数组来实现,链表则是由一系列不必在内存中相连的节点组成。每个节点均含有表元素以及指向后继节点的指针,我们称其为next指针。下面是单链表的c语言实现

头文件 linkedlist.h

struct Node;

typedef int Element;
typedef struct Node *ptrToNode;

//创建链表
ptrToNode createLinkList();
//清空链表
void makeEmpty(ptrToNode head);
//输出链表元素
void printList(ptrToNode head);
//链表是否为空
int isEmpty(ptrToNode head);
//删除元素e
void deleteElement(ptrToNode head,Element e);
//插入元素e
void insertElement(ptrToNode head,Element e);
//插入元素到index位置
void insertElement(ptrToNode head,Element e,int index);
//找到元素e的前一个节点
ptrToNode findPrevious(ptrToNode head,Element e); 
//找到Index位置的节点
ptrToNode findPtrByPosition(ptrToNode head,int index);
//找到最后节点
ptrToNode findLastPtr(ptrToNode head);
//销毁链表
void destoryLinkList(ptrToNode head);
//获得位置index的元素
Element getElement(ptrToNode head,int index);


具体实现
#include<stdio.h>
#include<stdlib.h>
#include "linkedlist.h"


struct Node{
	Element data;
	ptrToNode next;
};


//创建链表
ptrToNode createLinkList(){
	ptrToNode head = (ptrToNode)malloc(sizeof(struct Node));
	if(head == NULL){
		printf("malloc fail\n");
		return NULL;
	}
	head->next = NULL;
	return head;
}
//清空链表
void makeEmpty(ptrToNode head){
	if(head == NULL){
		printf("the linked list is not create\n");
		return;
	}
	ptrToNode p = head->next,temp;
	while(p!=NULL){
		temp = p->next;
		free(p);
		p = temp;
	}
	head->next = NULL;
}


//判断链表是否为空
int isEmpty(ptrToNode head){
	return head->next == NULL;
}


//打印链表
void printList(ptrToNode head){
	if(head == NULL){
		printf("the linked list is not create\n");
		return;
	}
	ptrToNode ptr = head->next;
	while(ptr!=NULL){
		printf("%d ",ptr->data);
		ptr = ptr->next;
	}
}


//删除元素
void deleteElement(ptrToNode head,Element e){
	ptrToNode pre = findPrevious(head,e);
	if(pre == NULL){
		return;
	}
	ptrToNode temp;


	temp = pre->next;
	pre->next = temp->next;
	free(temp);
}


//插入元素到末尾
void insertElement(ptrToNode head,Element e){
	ptrToNode temp,pre;
	temp = (ptrToNode)malloc(sizeof(struct Node));
	if(temp == NULL){
		printf("malloc fail\n");
		return;
	}
	temp->data = e;
	pre = findLastPtr(head);
	
	temp->next = pre->next;
	pre->next = temp;
}


//插入元素
void insertElement(ptrToNode head,Element e,int index){
	ptrToNode ptr,temp,pre;
	temp = (ptrToNode)malloc(sizeof(struct Node));
	if(temp == NULL){
		printf("malloc fail\n");
		return;
	}
	temp->data = e;
	ptr = findPtrByPosition(head,index);
	pre = findPrevious(head,ptr->data);


	temp->next = pre->next;
	pre->next = temp;
}


//找到元素的前驱
ptrToNode findPrevious(ptrToNode head,Element e){
	if(head == NULL){
		printf("the linked list is not create\n");
		return NULL;
	}
	ptrToNode ptr = head;
	while(ptr->next!=NULL&&ptr->next->data!=e){
		ptr = ptr->next;
	}
	return ptr;
}


//找到相应位置元素的节点指针
ptrToNode findPtrByPosition(ptrToNode head,int index){
	int i = 1;
	if(head == NULL){
		printf("the linked list is not create\n");
		return NULL;
	}
	ptrToNode ptr = head;
	for(i=1;i<=index&&ptr!=NULL;i++){
		ptr = ptr->next;
	}
	return ptr;
}


//找到末尾节点
ptrToNode findLastPtr(ptrToNode head){
	if(head == NULL){
		printf("the linked list is not create\n");
		return NULL;
	}
	ptrToNode ptr = head;
	while(ptr->next!=NULL)
		ptr = ptr->next;
	return ptr;
}


//销毁链表
void destoryLinkList(ptrToNode head){
	ptrToNode p = head,temp;
	while(p!=NULL){
		temp = p->next;
		free(p);
		p = temp;
	}
	head = NULL;
}


//找到下标为index的元素
Element getElement(ptrToNode head,int index){
	ptrToNode ptr;
	ptr = findPtrByPosition(head,index);
	if(ptr == NULL){
		printf("下标越界\n");
		return NULL;
	}
	return ptr->data;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值