数据结构循环单链表之(一)初始化&&尾插&&头插&&显示表&&尾删&&头删

前言

开始循环单链表!!!功能和单链表一样,这一部分包括初始化&&尾插&&头插&&显示表&&尾删&&头删

1. 思路

1.1 初始化

在这里插入图片描述

1.2 尾插

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

1.3 显示表

在这里插入图片描述

1.4 头插

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

1.5 尾删

在这里插入图片描述
在这里插入图片描述

1.6 头删

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. 代码

2.1 SCList.h

#ifndef _SCList_H_
#define _SCList_H_

#include <stdio.h>
#include <assert.h>
#include <malloc.h>

typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node *next;
}Node, *PNode;

typedef struct List
{
	PNode first;
	PNode last;
	int size;
}List;

//初始化
void InitSCList(List *list);

//显示链表
void show(List *list);

//尾插
void push_back(List *list, ElemType x);

//头插
void push_front(List *list, ElemType x);

//尾删
void pop_back(List *list);

//头删
void pop_front(List *list);

#endif //_SCList_H_

2.2 SCList.cpp

#include "SCList.h"

void InitSCList(List *list)
{
	Node *s   = (Node *)malloc(sizeof(Node));
	assert(s != NULL);
	list->first      = list->last = s;
	list->last->next = list->first;
	list->size       = 0;
}

void show(List *list)
{
	Node *s = list->first->next;
	while(s != list->first)
	{
		printf("%d-->", s->data);
		s = s->next;	
	}
	printf("NUL.\n");
}

Node* buy_node(ElemType x)
{
	Node *s = (Node *)malloc(sizeof(Node));
	assert(s != NULL);
	s->data = x;
	s->next = NULL;
	return s;
}

void push_back(List *list, ElemType x)
{
	Node *s = buy_node(x);
	list->last->next = s;
	list->last       = s;
	list->last->next = list->first;
	list->size++;
}



void push_front(List *list, ElemType x)
{
	Node *s           = buy_node(x);
	s->next           = list->first->next;
	list->first->next = s; 
	if(list->size == 0)
	{
		list->last = s;
		list->last->next = list->first;
	}
	list->size++;
}


void pop_back(List *list)
{
	if(list->size == 0)
	{
		printf("链表已空,无法删除。\n");
		return;
	}
	Node *s = list->last;
	while(s->next != list->last)
	{
		s = s->next;
	}  
	free(s->next);
	list->last = s;
	list->last->next = list->first;
}	
	

void pop_front(List *list)
{
		
	if(list->size == 0)
	{
		printf("链表已空,无法删除。\n");
		return;
	}
	Node *s = list->first->next;
	free(list->first->next);
	list->first->next = s->next;
	if(list->size == 1)
	{
		list->last = list->first;
		list->last->next = list->first;
	}
	list->size--;
}

2.3 main

#include "SCList.h"

int main()
{
	List mylist;
	InitSCList(&mylist);

	ElemType Item;
	int select = 1;
	while(select)
	{
		printf("*********************************\n");
		printf("* [1] push_back  [2] push_front *\n");
		printf("* [3] show_list  [4] pop_back   *\n");
		printf("* [5] pop_front  [6] insert_val *\n");
		printf("* [7] find       [8] length     *\n");
		printf("* [9] delete_val [10] sort      *\n");
		printf("* [11] resver    [12] clear     *\n");
		printf("* [13] destroy   [0] quit       *\n");
		printf("*********************************\n");
		printf("请选择:>");
		scanf("%d", &select);
		
		switch(select)
		{
			case 1:
				printf("请输入要插入的数据(-1结束):");
				while(scanf("%d",&Item), Item != -1)
				{
					push_back(&mylist, Item);
				}
				break;			
			case 2:			
				printf("请输入要插入的数据(-1结束):");
				while(scanf("%d",&Item), Item != -1)
				{
					push_front(&mylist, Item);
				}
				break;
			case 3:
				show(&mylist);
				break;
			case 4:
				pop_back(&mylist);
				break;
			case 5:
 				pop_front(&mylist);
				break;
			default:
				break;
		}
		
	}
	return 0;
}

3. 结果

3.1 尾插和显示表

在这里插入图片描述

3.2 头插

在这里插入图片描述

3.3 尾删和头删

在这里插入图片描述

总结

接下来做剩下的部分。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值