首页 > 其他分享 >双链表和队列-->gcc编译

双链表和队列-->gcc编译

时间:2023-05-10 15:01:07浏览次数:54  
标签:gcc curr -- void head next LTNode que 双链

双链表
队列

doublueList.h

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

typedef int LTDataType;

typedef struct DList
{
	LTDataType data;
	struct DList *next;
	struct DList *prev;
}LTNode;

LTNode* init();//创建哨兵位  (自定义头节点)

LTNode* createNode(LTDataType x);//创建新节点

void pushFront(LTNode* head,LTDataType x);//头插

void popFront(LTNode* head);

void pushBack(LTNode* head,LTDataType x);//尾插

void popBack(LTNode* head);

void findTarget(LTNode* head,LTDataType x);

void insert(LTNode* pos,LTDataType x);

void delete(LTNode* pos);

int size(LTNode* head);

void destory(LTNode* head);


void printfDouble(LTNode* head);//打印



//队列:先进先出,后进后出
typedef int QDataType;

// (单链表)链式结构:表示队列
typedef struct QueueNode
{
	QDataType data;
	struct QueueNode *next;
}QueueNode;

//队列
typedef struct Queue
{
	QueueNode *head;//队头
	QueueNode *tail;//对尾
}Queue;


void initQueue(Queue *que);

bool emptyQueue(Queue *que);

void pushQueue(Queue *que,QDataType data);//入队 -->尾插

void popQueue(Queue *que,QDataType data);//出队  -->头删

QDataType getFront(Queue *que);//获取队列头部元素

QDataType getBack(Queue *que);//获取队列队尾元素

int QueueSize(Queue *que);

void destoryQueue(Queue *que);

doublueList.c

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#include "doubleList.h"

LTNode* createNode(LTDataType x)
{
	LTNode* newCode = (LTNode*)malloc(sizeof(LTNode));
	
	newCode->data = x;
	newCode->next = NULL;
	newCode->prev = NULL;
	return newCode;
}

LTNode* init()
{
	LTNode* head = createNode(-1);
	head->prev = head;
	head->next = head;
	
	return head;
}

//LTNode* pos传进来的是 当前链表的首地址 ,如何你建立了哨兵 那就是哨兵的地址
void insert(LTNode* pos,LTDataType x)
{
	assert(pos);
	
	LTNode* newNode = createNode(x);
	LTNode* pos_prev = pos->prev;
	
	newNode->prev = pos_prev;
	newNode->next = pos;
	
	pos_prev->next = newNode;
	pos->prev = newNode;
}

void delete(LTNode* pos)
{
	assert(pos);
	assert(pos!=pos->next);
	
	pos->next->prev = pos->prev;
	pos->prev->next = pos->next;
	free(pos);
}

void pushFront(LTNode* head,LTDataType x)
{
	insert(head->next,x);	
}

void pushBack(LTNode* head,LTDataType x)
{
	insert(head,x);	
}

void popFront(LTNode* head)
{
	delete(head->next);
}

void popBack(LTNode* head)
{
	delete(head->prev);
}


int size(LTNode* head)
{
	LTNode* curr = head->next;
	int size = 0;
	while(curr != head)
	{
		size++;
		curr = curr->next;
	}
	return size;
}

void destory(LTNode* head)
{
	LTNode* curr = head->next;
	while(curr != head)
	{
		LTNode* curr_next = curr->next;
		free(curr);
		curr = curr_next;
	}
	free(head);
}

void printfDouble(LTNode* head)
{
	assert(head);
	int x = 0;
	while(head)
	{
		if(x == 2)
		{
			break;
		}
		if(head->data == -1)
		{
			x++;
		}
		else
		{
			printf("%d ",head->data);
		}
		head = head->next;
	}
	printf("/n");
	
	getchar();
}

/***********************************************/

void initQueue(Queue *que)
{
	assert(que);
	que->head = que->tail = NULL;
}


bool emptyQueue(Queue *que)
{
	assert(que);
	return que->head == NULL && que->tail == NULL;
}

void pushQueue(Queue *que,QDataType data)
{
	assert(que);
	QueueNode *newNode = (QueueNode*)malloc(sizeof(QueueNode));
	
	if(newNode == NULL)
	{
		
	}
	else
	{
		newNode->next = NULL;
		newNode->data = data;
	}
	
	if(que->head == NULL)
	{
		que->head = que->tail = newNode;
	}
	else
	{
		que->tail->next = newNode;
		que->tail = que->tail->next;
	}
}


void popQueue(Queue *que,QDataType data)
{
	assert(que);
	assert(emptyQueue(que));
	
	if(que->head->next == NULL)
	{
		free(que->head);
		que->head = que->tail= NULL;
	}
	else
	{
		QueueNode* twice = que->head->next;
		free(que->head);
		que->head = twice;	
	}
}

QDataType getFront(Queue *que)
{
	assert(que);
	assert(!emptyQueue(que));
	return que->head->data;
}

QDataType getBack(Queue *que)
{
	assert(que);
    assert(!emptyQueue(que));
    return que->tail->data;
}

int QueueSize(Queue *que)
{
	assert(que);
	QueueNode* curr = que->head;
	int size = 0;
	while(curr)
	{
		curr = curr->next;
		size++;
	}
	return size;
}

void destoryQueue(Queue *que)
{
	assert(que);
	QueueNode* curr = que->head;
	while(curr)
	{
		QueueNode *del = curr;
		curr = curr->next;
		free(del);
	}
	
	que->head = que->tail = NULL;
}

void printfQueue(Queue *que)
{
	assert(que);
	QueueNode* curr = que->head;
	while(curr)
	{
		printf("%d ",curr->data);
		curr = curr->next;
	}
	
}

doublueText.c

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include "doubleList.h"


/*
lomo_t@lomo-unix:~/rubbish_box/list_box$ gcc -o doubleText doubleList.c doubleText.c 
lomo_t@lomo-unix:~/rubbish_box/list_box$ ./doubleText 
destory --> size : 3 
QDataType --> getFront : 2 
*/


LTNode* SL  = NULL;

Queue* QU = NULL;
void Text()
{
	/*SL = init();
	
	insert(SL,1);
	insert(SL,3);
	insert(SL,5);
	pushFront(SL,2);
	printfDouble(SL);
	
	popFront(SL);
	printfDouble(SL);
	
	printf("size : %d \n",size(SL));
	
	destory(SL);*/
	
	//printf("destory --> size : %d \n",size(SL));
	
	QU = (Queue*)malloc(sizeof(Queue));
	
	initQueue(QU);
	pushQueue(QU,2);
	pushQueue(QU,4);
	pushQueue(QU,6);
	
	QDataType x = getFront(QU);
	
	printf("destory --> size : %d \n",QueueSize(QU));
	
	printf("QDataType --> getFront : %d \n",x);
}

int main()
{
	Text();
	return 0;
}

标签:gcc,curr,--,void,head,next,LTNode,que,双链
From: https://www.cnblogs.com/kato-T/p/17387987.html

相关文章

  • 快速访问element-ui
    第一步:点击下方前往“站长工具”查询对应网址youzan.github.io的DNS站长工具地址 第二步:找到TTL值最小的那个的ip地址和我们需要的网址对应关系,配置到hosts文件中win10hosts文件默认路径:C:\Windows\System32\drivers\etc此处点击文件、打开Windowspowershell以管理员身份打......
  • fatal: unable to access 'https://gitee.com/...': Could not resolve host: gitee.c
    把https模式换成ssh用gitremote-v查看使用的是https还是ssh等$gitremote-v>originhttps://github.com/USERNAME/REPOSITORY.git(fetch)>originhttps://github.com/USERNAME/REPOSITORY.git(push)使用gitremoteset-url命令将远程URL从HTTPS更改为SSH$gitremote......
  • 蚁景科技受邀参加中国刑事警察学院学科专业建设研讨会
    5月7日,湖南蚁景科技有限公司受邀出席中国刑事警察学院公安信息技术与情报学院学科专业建设研讨会。本次研讨会由中国刑事警察学院主办,公安信息技术与情报学院、教育部网络安全与执法专业虚拟教研室、辽宁网络安全执法协同创新中心、网络安全执法与视频侦查辽宁省重点实验室协办。......
  • mysql中删除时报错Cannot truncate a table referenced in a foreign key constraint
    在Mysql使用Truncate截断表时,提示Cannottruncateatablereferencedinaforeignkeyconstraint(monitoritem,CONSTRAINTmonitortaskpollutant_monitortask_fk)。这是因为存在外键约束导致的无法删除,我们可以先关闭外键约束,删除后再启动外键约束。1、检查外键约束SELE......
  • 如何使用ChatGPT提高数据库效率520倍
    在过去的几个月里,随着ChatGPT、Midjourney、StableDiffusion等国外产品的快速迭代,以及国内百度、阿里、飞书、网易等大厂发布的大模型,一些设计、研发、自媒体从业者开始感到自危,仿佛他们的工作在AI的洪流中瞬间会被取代。那人人都能做的产品经理(bushi),在这次的AI革命中能做什么?我......
  • 学习video相关事件及vue中监听切出页面方法
    1.vue中监听切出页面方法使用到的事件为:visibilitychangevisibilitychange是浏览器新添加的一个事件,当浏览器当前页面被最小化或切换浏览器其他标签页后者从其他页面或应用返回到当前标签页都会触发这个事件。document.visibilityState共有四个值:hidden:文档处于背景标签......
  • 浏览器设置广告拦截
    打开系统自带浏览器点击右边...选择扩展   进入扩展页面后搜索adguard后下载插件开启拦截功能随便打开一个网页测试拦截功能......
  • Postgresql insert on conflict笔记
    描述针对数据写入时有主键冲突的情况,INSERTONCONFLICT语法可以将冲突主键的INSERT行为转换为UPDATE行为,从而实现冲突主键的覆盖写入。该特性又称UPSERT覆盖写,与MySQL的REPLACEINTO类似。[WITH[RECURSIVE]with_query[,...]]INSERTINTOtable_name[ASalias][......
  • SQLSERVER获取汉字拼音码
     用户的输入经常用到拼音码选择器 --调用方法select dbo.wsh_GetPY('汉字')ALTERFUNCTION[dbo].[wsh_GetPY](@strNVARCHAR(4000))RETURNSNVARCHAR(4000)ASBEGINDECLARE@WORDNCHAR(1),@PYNVARCHAR(4000)SET@PY=''WHILELEN(@STR)>0BEGI......
  • java8一个List转化为另外一个List
    List<String>filterTags=Lists.newArrayList();List<Promotion>promotionList=filterTags.stream().map(f->{Promotiontag=newPromotion(context);tag.setLabel(f);tag.setCode(f);......