国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

數(shù)據(jù)結(jié)構(gòu)day05(單鏈表)

這篇具有很好參考價(jià)值的文章主要介紹了數(shù)據(jù)結(jié)構(gòu)day05(單鏈表)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

今日任務(wù):

數(shù)據(jù)結(jié)構(gòu)day05(單鏈表),數(shù)據(jù)結(jié)構(gòu)

?思維導(dǎo)圖:

數(shù)據(jù)結(jié)構(gòu)day05(單鏈表),數(shù)據(jù)結(jié)構(gòu)

實(shí)現(xiàn) 代碼:(多文件)

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int datatype;

typedef struct Linklist
{
	union {
		int len;
		datatype data;
	};
	struct Linklist* next;
}Node,*NodeP;
NodeP head_create();
NodeP create();
int output(NodeP head);
int tail_insert(NodeP head,datatype data);
int head_insert(NodeP head,datatype data);
int tail_delete(NodeP head);
int head_delete(NodeP head);
int pos_insert(NodeP head,datatype data,int pos);
int pos_delete(NodeP head,int pos);
int pos_update(NodeP head,datatype data,int pos);
int value_index(NodeP head,datatype data);
int value_delete(NodeP head,datatype data);
int inversion(NodeP head);
int free_linklist(NodeP head);
#endif

fun.c

#include "head.h"
/*
 * function:   傳參 空指針判定
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int void_point(NodeP p){
	if(NULL==p){
		puts("how dare you give me null point.");
		return -1;
	}
	return 0;
}
/*
 * function:    頭結(jié)點(diǎn)創(chuàng)建
 * @param [ in] 
 * @param [out] 
 * @return      
 */
NodeP head_create(){
	NodeP head=(NodeP)malloc(sizeof(Node));
	if(head==NULL){
		puts("how dare you give me null place.");
		return NULL;
	}
	head->len=0;
	head->next=NULL;
	return head;
}
/*
 * function:    節(jié)點(diǎn)創(chuàng)建
 * @param [ in] 
 * @param [out] 
 * @return      
 */
NodeP create(datatype data){
	NodeP new=(NodeP)malloc(sizeof(Node));
	if(new==NULL){
		puts("how dare you give me null place.");
		return NULL;
	}
	new->data=data;
	new->next=NULL;
	return new;
}
/*
 * function:    輸出
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int output(NodeP head){
	if(void_point(head))
		return -1;
	while(head->next!=NULL){
		printf("%d\t",head->next->data);
		head=head->next;
	}
	puts("output done.");
}
/*
 * function:    節(jié)點(diǎn)尾插
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int tail_insert(NodeP head,datatype data){
	if(void_point(head))
		return -1;
	//找到尾部節(jié)點(diǎn)
	NodeP p=head;
	while(p->next!=NULL)
		p=p->next;
	p->next=create(data);
	head->len++;
	puts("tail insert success.");
	return 0;
}
/*
 * function:    節(jié)點(diǎn)頭插
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int head_insert(NodeP head,datatype data){
	if(void_point(head))
		return -1;
	NodeP new=create(data);
	new->next=head->next;
	head->next=new;
	head->len++;
	puts("head insert success");
}
/*
 * function:    尾刪
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int tail_delete(NodeP head){
	if(void_point(head))
		return -1;
	if(head->next==NULL){
		puts("there is no assigment to delete.");
		return -1;
	}
	//找到最后一個(gè)節(jié)點(diǎn),free并len--,前一節(jié)點(diǎn)指向null
	NodeP p=head;
	while(p->next->next!=NULL)
		p=p->next;
	free(p->next);
	p->next=NULL;
	head->len--;
	puts("tail delete success");
	return 0;
}
/*
 * function:    頭刪
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int head_delete(NodeP head){
	if(void_point(head))
		return -1;
	if(head->next==NULL){
		puts("there is no assigment to delete.");
		return -1;
	}
	NodeP p=head->next;
	free(head->next);
	head->next=p->next;
	p=NULL;
	head->len--;
	puts("head delete success");
	return 0;
}
/*
 * function:    指定位置添加
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int pos_insert(NodeP head,datatype data,int pos){

	if(void_point(head))
		return -1;
	if(pos>head->len+1||pos<1){
		puts("your position is illegal.");
		return -1;
	}
	NodeP p=head;
	while(pos--!=1)
		p=p->next;
	NodeP new=create(data);
	new->next=p->next;
	p->next=new;
	head->len++;
	puts("pos insert success");
}
/*
 * function:    指定位置刪除
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int pos_delete(NodeP head,int pos){
	if(void_point(head))
		return -1;
	if(pos<1||pos>head->len){
		puts("your position is illegal.");
		return -1;
	}
	NodeP p=head;
	while(pos--!=1)
		p=p->next;
	NodeP x=p->next;
	p->next=p->next->next;
	free(x);
	x=NULL;
	head->len--;
	puts("pos delete success");
	return 0;
}
/*
 * function:    指定位置修改
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int pos_update(NodeP head,datatype data,int pos){
	if(void_point(head))
		return -1;
	if(pos<1||pos>head->len){
		puts("your position is illegal.");
		return -1;
	}
	NodeP p=head;
	while(pos--)
		p=p->next;
	p->data=data;
	puts("pos update success");
	return 0;
}
/*
 * function:    按值查找下表
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int value_index(NodeP head,datatype data){
	if(void_point(head))
		return -1;
	if(head->len==0){
		puts("linklist is null");
		return -1;
	}
	int index=0;
	NodeP p=head;
	while(p->next!=NULL){
		p=p->next;
		index++;
		if(p->data==data){
			printf("the index your want to find is%d\n",index);
			return index;
		}
	}
	puts("can't find your value");
	return 0;
}
/*
 * function:    按值刪除
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int value_delete(NodeP head,datatype data){
	if(void_point(head))
		return -1;
	if(head->len==0){
		puts("linklist is null");
		return -1;
	}
	NodeP p=head;
	while(p->next!=NULL){
		if(p->next->data==data){
			pos_delete(head,value_index(head,data));
			puts("value delete success");
			return 0;
		}
		p=p->next;
	}
	puts("no value your want to delete");
}
/*
 * function:    循環(huán)逆置
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int inversion(NodeP head){
	if(void_point(head))
		return -1;
	if(head->len==0){
		puts("linklist is NULL");
		return -1;
	}
	if(head->len==1){
		puts("there is no deed");
		return 0;
	}
	//逆置
	//將第二個(gè)節(jié)點(diǎn)作為尾節(jié)點(diǎn),但是得先記錄一下,還得用于每次循環(huán)的頭插的第一個(gè)元素
	NodeP p=head->next->next;
	head->next->next=NULL;
	//定義一個(gè)節(jié)點(diǎn),用于方便循環(huán)調(diào)用后面的元素頭插,
	NodeP k=p;
	puts("debug..");
	output(p);
	while(p!=NULL){
		p=p->next;

		k->next=head->next;
		head->next=k;

		k=p;
	}
	puts("inversion success.");
	return 0;
}
/*
 * function:    釋放鏈表
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int free_linklist(NodeP head){
	if(void_point(head))
		return -1;
	NodeP p=NULL;
	while(head!=NULL){
		p=head;
		head=head->next;
		free(p);
		p=NULL;
	}
	puts("free success.");
	return 0;
}

main.c

#include "head.h"
int main(int argc, const char *argv[])
{
	NodeP p=head_create();
	tail_insert(p,10);
	tail_insert(p,20);
	tail_insert(p,30);
	tail_insert(p,40);
	tail_insert(p,50);
	head_insert(p,99);
	head_insert(p,88);
	output(p);
	//pos_insert(p,66,0);
	//output(p);
	//pos_delete(p,8);
	//output(p);
	//pos_update(p,66,8);
	//output(p);
	//tail_delete(p);
	//output(p);
	//head_delete(p);
	//output(p);
	//value_delete(p,77);
	//output(p);
	//inversion(p);
	//output(p);
	free_linklist(p);
	p=NULL;
	return 0;
}

不好,眼花了,沒看到實(shí)現(xiàn)單項(xiàng)循環(huán)鏈表,ji文章來源地址http://www.zghlxwxcb.cn/news/detail-682377.html

到了這里,關(guān)于數(shù)據(jù)結(jié)構(gòu)day05(單鏈表)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 數(shù)據(jù)結(jié)構(gòu)——單鏈表(上)

    數(shù)據(jù)結(jié)構(gòu)——單鏈表(上)

    ??個(gè)人主頁:_麥麥_ ??今日名言:“生活總是讓我們遍體鱗傷,但到后來,那些受傷的地方一定會變成我們最強(qiáng)壯的地方?!????????——海明威《永別了武器》 目錄 ?編輯 一、前言 二、正言 ?3.1鏈表的概念及結(jié)構(gòu) 3.2鏈表的分類 3.3鏈表的實(shí)現(xiàn)【無頭單向非循環(huán)】 3.

    2024年02月02日
    瀏覽(20)
  • 數(shù)據(jù)結(jié)構(gòu)——單鏈表

    數(shù)據(jù)結(jié)構(gòu)——單鏈表

    我們需要在程序中存儲一系列的數(shù)據(jù),這些數(shù)據(jù)不是一成不變的,需要滿足隨時(shí)的動態(tài)增刪查改。 順序表,雖然能滿足這些要求,可是順序表在頭插或者中間插入數(shù)據(jù)時(shí),需要將數(shù)據(jù)一個(gè)一個(gè)挪動,效率較低;而且需要頻繁的擴(kuò)容,擴(kuò)容也是需要付出一定的代價(jià)。 為有效解

    2023年04月17日
    瀏覽(22)
  • 【數(shù)據(jù)結(jié)構(gòu)】單鏈表詳解

    【數(shù)據(jù)結(jié)構(gòu)】單鏈表詳解

    當(dāng)我們學(xué)完順序表的時(shí)候,我們發(fā)現(xiàn)了好多問題如下: 中間/頭部的插入刪除,時(shí)間復(fù)雜度為O(N) 增容需要申請新空間,拷貝數(shù)據(jù),釋放舊空間。會有不小的消耗。 增容一般是呈2倍的增長,勢必會有一定的空間浪費(fèi)。例如當(dāng)前容量為100,滿了以后增容到200,我們 再繼續(xù)插入了

    2024年02月09日
    瀏覽(25)
  • 數(shù)據(jù)結(jié)構(gòu)--單鏈表

    數(shù)據(jù)結(jié)構(gòu)--單鏈表

    目錄 1.單鏈表的定義: 單鏈表基本操作的實(shí)現(xiàn): 2.單鏈表的初始化(構(gòu)造帶頭結(jié)點(diǎn)的空表) 2.將頭結(jié)點(diǎn)的指針域置空 3.鏈表是否為空: 4.單鏈表的銷毀: 5.單鏈表的清空: 6.求單鏈表的表長: 7.? ?取值? 取單鏈表第i個(gè)元素: 8按值查找 根據(jù)指定數(shù)據(jù)查找指定數(shù)據(jù)所在位置序

    2024年02月15日
    瀏覽(22)
  • 數(shù)據(jù)結(jié)構(gòu)-單鏈表

    數(shù)據(jù)結(jié)構(gòu)-單鏈表

    概念:鏈表是一種物理存儲結(jié)構(gòu)上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的 。 ?從以上圖片可以看出: 1.鏈?zhǔn)浇Y(jié)構(gòu)在邏輯上是連續(xù)的,但在物理上不一定是連續(xù)的。 2.現(xiàn)實(shí)中的節(jié)點(diǎn)一般是在堆上申請出來的。 3.從堆上申請的空間,

    2024年02月05日
    瀏覽(21)
  • 【數(shù)據(jù)結(jié)構(gòu)】單鏈表(超全)

    【數(shù)據(jù)結(jié)構(gòu)】單鏈表(超全)

    前言: ?上一次我們分享了線性表中的一種結(jié)構(gòu)順序表,它存在著一些其缺點(diǎn),比如:在中間位置或者頭部進(jìn)行元素插入或者刪除的時(shí)候時(shí)間復(fù)雜度是 O ( N ) O(N) O ( N ) 效率比較低,并且順序表在擴(kuò)容的時(shí)候也存在時(shí)間和空間上的消耗,由于我們每次都是按照二倍來擴(kuò)的,那

    2024年02月11日
    瀏覽(28)
  • 數(shù)據(jù)結(jié)構(gòu)入門——單鏈表

    目錄 前言 鏈表的定義 鏈表的創(chuàng)建 頭插法創(chuàng)建鏈表 尾插法創(chuàng)建鏈表 鏈表的刪除 在頭部刪除元素 在尾部刪除元素 查找固定元素 在鏈表中插入元素 在pos位置前插入 在pos位置后插入 刪除鏈表結(jié)點(diǎn) 刪除pos位置的結(jié)點(diǎn) 刪除pos后的結(jié)點(diǎn) 鏈表的銷毀 寫在最后 前面我們學(xué)習(xí)了順序表

    2024年02月20日
    瀏覽(20)
  • 數(shù)據(jù)結(jié)構(gòu)—單鏈表

    數(shù)據(jù)結(jié)構(gòu)—單鏈表

    目錄 1.前言 2.了解單鏈表 3.單鏈表代碼實(shí)現(xiàn) ? ? ? 3.1?單鏈表結(jié)構(gòu)體實(shí)現(xiàn) ? ? ? 3.2?創(chuàng)建節(jié)點(diǎn) ? ? ? 3.3? 打印單鏈表 ? ? ? 3.4?尾插? ? ? ? 3.5? 頭插? ? ? ? 3. 6 頭刪 ? ? ? 3.7? 尾刪 ? ? ? 3.8? 查找 ? ? ? 3.9? 插入 ? ? ? ? ? ?3.9.1 在pos位置之前插入? ? ? ? ? ? ?3.9.

    2023年04月20日
    瀏覽(19)
  • 【數(shù)據(jù)結(jié)構(gòu)】單鏈表(詳解)

    【數(shù)據(jù)結(jié)構(gòu)】單鏈表(詳解)

    所屬專欄:初始數(shù)據(jù)結(jié)構(gòu) 博主首頁:初陽785 代碼托管:chuyang785 感謝大家的支持,您的點(diǎn)贊和關(guān)注是對我最大的支持?。。?博主也會更加的努力,創(chuàng)作出更優(yōu)質(zhì)的博文??! 關(guān)注我,關(guān)注我,關(guān)注我,重要的事情說三遍!?。。。。。?! 前面我們已經(jīng)用順序表方式來實(shí)現(xiàn)接口

    2023年04月24日
    瀏覽(23)
  • 【數(shù)據(jù)結(jié)構(gòu)】單鏈表(一)

    【數(shù)據(jù)結(jié)構(gòu)】單鏈表(一)

    作者:日出等日落 專欄:數(shù)據(jù)結(jié)構(gòu) 想變成仲夏夜的一只螢火蟲,只要抓住你的注意力,就已經(jīng)滿足了。 目錄 前言: ?單鏈表的結(jié)構(gòu) : ?邏輯結(jié)構(gòu): 物理結(jié)構(gòu): BuySLTNode(動態(tài)申請一個(gè)結(jié)點(diǎn)):? ?CreateSList(//循環(huán)創(chuàng)建結(jié)點(diǎn)): SLTPrint(單鏈表打印): ?單鏈表的功能:? SL

    2024年02月01日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包