04.20:单链表和单循环链表(打卡第三天)

一,线性表:

1.有限的序列; 2.序列中每一个元素都有唯一一个前驱和后继结点(除开头和结尾两个结点)

顺序表:分配一块连续的内存去存放这些元素,例如编程中的数组

链表:内存是不连续的,元素会各自被分配一块内存,内存和内存之间用指针相连。

二,单链表的操作:

1.增加

a.头插法

b.尾插法

2.删除  (只需要找到对应结点,将对应结点的前一个结点指向结点的后继,只操作一个指针)

以下是具体实现的代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
	int data;   //数据域 
	struct Node* next; //指针域 
}Node;   //单链表中结点类型的描述 

Node* initList(){    //初始化头结点 
	Node* list = (Node*)malloc(sizeof(Node));    //开辟空间 
	list->data=0;          
	list->next=NULL;
	return list;
}

void headInsert (Node* list, int data)    //头插法 
{
	Node* node = (Node*)malloc(sizeof(Node));  //开辟空间
	node->data = data;
	node->next = list->next;
	list->next=node;
	list->data++;
}

void tailInsert(Node* list,int data)   //尾插法 
{
	Node* head = list;
	Node* node = (Node*)malloc(sizeof(Node));  //开辟空间
	node->data = data;
	node->next = NULL;
	list = list->next;
	while (list->next){
		list = list->next;
	}
	list->next = node;
	head->data++;
 } 
 
void remove(Node* list,int data)    //结点的删除 
{
	Node* pre = list;
	Node* current = list->next;
	while(current){
		if(current->data == data){
			pre->next = current->next;
			free(current);
			break;
		}
		pre  = current;
	current = current->next;
	}
	list->data--;
}

void printList(Node* list){      //遍历 
	 list = list->next;
	 while(list){
	 	printf("%d", list->data);
	 	list = list->next;
	 } 
	 printf("\n");
}

int main()
{
 	Node* list = initList();
 	headInsert(list,1);
 	headInsert(list,2);
 	headInsert(list,3);
 	headInsert(list,4);
 	headInsert(list,5);
 	tailInsert(list,6);
 	tailInsert(list,7);
 	tailInsert(list,8);
 	tailInsert(list,9);
 	tailInsert(list,10);
 	printf("此时单链表为: \n"); 
 	printList(list);
 	remove(list,5);
 	remove(list,10);
 	remove(list,6);
 	printf("删除后的单链表为:\n");
    printList(list);;
 	return 0;
}

输出结果:

三,单循环链表

基本操作:

1.初始化链表

2.增加结点

3.删除结点

4.遍历链表

代码实现如下:

#include <stdio.h>
#include<stdlib.h>

#define TRUE  1
#define FALSE 0  

typedef struct Node{
    int data;
    struct Node* next;
}Node;          //定义单循环链表结构体

//单循环链表初始化 
Node* initList(){
	Node* L = (Node*)malloc(sizeof(Node));
	L->data = 0;
	L->next = L;
	return L; 
} 

//头插入法
void headInsert(Node* L,int data){
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	node->next = L->next;
	L->next = node;
}

//尾插入法
void tailInsert(Node* L,int data){
	Node* n = L; 
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	while(n->next!=L){
		n = n->next;
	}
	node->next = L;
	n->next = node;
}

//删除结点功能,删除可能会成功也可能会失败,所以得定义几个宏变量来声明
int remove(Node* L,int data){
	Node* preNode = L; 
	Node* node = L->next;
	while(node!=L){
		if(node->data == data){
			//delete
			preNode->next = node->next;
			free(node); 
			return TRUE;
		}
		preNode = node;
		node = node->next;
	}
	return FALSE;
} 

 
void printList(Node* L){   //遍历 
	Node* node = L->next;
	while(node!=L)
	{
		printf("%d->",node->data);
		node = node->next;
	}
	printf("NULL\n");
}


int main()
{
	Node* L = initList();
	headInsert(L,1);
	headInsert(L,2);
	headInsert(L,3);
	headInsert(L,4);
	headInsert(L,5);
    printf("头插入法结束后为:\n");
    printList(L); 
    tailInsert(L,6);
	tailInsert(L,7);
	printf("尾插入法结束后为:\n");
    printList(L); 
    remove(L,4);
   	printf("删除节点后为:\n");
    printList(L); 
    return 0;
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值