亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語言數(shù)據(jù)結(jié)構(gòu)之順序表和單鏈表

 更新時(shí)間:2021年06月27日 08:53:38   作者:Ja_king_  
在數(shù)據(jù)結(jié)構(gòu)中,線性表是入門級(jí)數(shù)據(jù)結(jié)構(gòu),線性表又分為順序表和鏈表,這篇文章主要給大家介紹了關(guān)于C語言數(shù)據(jù)結(jié)構(gòu)之順序表和單鏈表的相關(guān)資料,需要的朋友可以參考下

一、順序表的創(chuàng)建、刪除和插入

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct sqlist {
	int date[10];
	int length;
};
void InitList(sqlist& L) {
	for (int i = 0;i < 10;i++) {
		L.date[i] = 0;
	}
	L.length = 0;
}
void charu(sqlist& L) {
	for (int j = 0;j < 5;j++) {
		scanf("%d", &L.date[j]);
		L.length++;
	}
}
void ListInsert(sqlist& L, int i, int e) {
	for (int k = L.length;k >= i;k--) {
		L.date[k] = L.date[k - 1];
	}
	L.date[i - 1] = e;
	L.length++;
}
void print(sqlist& L) {
	for (int i = 0;i < L.length;i++) {
		printf("%d ", L.date[i]);
	}
	printf("\n");
}
void ListDelete(sqlist& L, int i, int e) {
	for (int j = i;j < L.length;j++) {
		L.date[j-1] = L.date[j];
	}
	L.length--;
}
int main() {
	sqlist L;//創(chuàng)建順序表L
	InitList(L);//初始化順序表
	shuru(L);//輸入值
	ListInsert(L, 3, 3);//插入值
	print(L);//打印
	ListDelete(L, 3, 3);//刪除值
	print(L);
	return 0;
}

以上操作分別實(shí)現(xiàn)了對(duì)順序表的創(chuàng)建,插入,刪除和打印

二、單鏈表的創(chuàng)建、刪除、增加和輸出

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
    int num;
    struct ListNode* next;
};
struct ListNode* create(struct ListNode* head) {
    struct ListNode * p1, * p2;
    p1 = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    scanf("%d", &p1->num);
    while (p1->num!=0){
        if (head == NULL) {
            head = p1;
        }
        else {
            p2->next = p1;
        }
        p2 = p1;
        p1= (struct ListNode*)malloc(sizeof(struct ListNode));
        scanf("%d", &p1->num);
    }
    p2->next = NULL;
    free(p1);
    return head;
}
void print(struct ListNode* head) {
    while (head != NULL) {
        printf("%d ", head->num);
        head = head->next;
    }
}
int main() {
    struct ListNode* head=NULL;
    head=create(head);//創(chuàng)建鏈表
    print(head);//輸出鏈表
	return 0;
}

以上操作為創(chuàng)建鏈表并打印,效果如下:

現(xiàn)在增加插入操作

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
    int num;
    struct ListNode* next;
};
struct ListNode* create(struct ListNode* head) {
    struct ListNode * p1, * p2;
    p1 = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    scanf("%d", &p1->num);
    while (p1->num!=0){
        if (head == NULL) {
            head = p1;
        }
        else {
            p2->next = p1;
        }
        p2 = p1;
        p1= (struct ListNode*)malloc(sizeof(struct ListNode));
        scanf("%d", &p1->num);
    }
    p2->next = NULL;
    free(p1);
    return head;
}
void print(struct ListNode* head) {
    while (head != NULL) {
        printf("%d ", head->num);
        head = head->next;
    }
    printf("\n");
}
struct ListNode* insert(struct ListNode* head,int i) {
    struct ListNode* p1,*p2,*p;
    p1 =p2= head;
    for (int j = 1;j < i;j++) {
        p2 = p1;
        p1 = p1->next;
    }
    p= (struct ListNode*)malloc(sizeof(struct ListNode));
    printf("請(qǐng)輸入插入的數(shù):");
    scanf("%d", &p->num);
    p2->next = p;
    p->next = p1;
    return head;
}
int main() {
    struct ListNode* head=NULL;
    int a, b;
    head=create(head);
    print(head);
    printf("請(qǐng)輸入插入位置:");
    scanf("%d", &a);
    head = insert(head,a);//插入新數(shù)據(jù)
    print(head);
	return 0;
}

效果如下:

現(xiàn)增加刪除操作

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
    int num;
    struct ListNode* next;
};
struct ListNode* create(struct ListNode* head) {
    struct ListNode * p1, * p2;
    p1 = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    scanf("%d", &p1->num);
    while (p1->num!=0){
        if (head == NULL) {
            head = p1;
        }
        else {
            p2->next = p1;
        }
        p2 = p1;
        p1= (struct ListNode*)malloc(sizeof(struct ListNode));
        scanf("%d", &p1->num);
    }
    p2->next = NULL;
    free(p1);
    return head;
}
void print(struct ListNode* head) {
    while (head != NULL) {
        printf("%d ", head->num);
        head = head->next;
    }
    printf("\n");
}
struct ListNode* insert(struct ListNode* head,int i) {
    struct ListNode* p1,*p2,*p;
    p1 =p2= head;
    for (int j = 1;j < i;j++) {
        p2 = p1;
        p1 = p1->next;
    }
    p= (struct ListNode*)malloc(sizeof(struct ListNode));
    printf("請(qǐng)輸入插入的數(shù):");
    scanf("%d", &p->num);
    p2->next = p;
    p->next = p1;
    return head;
}
struct ListNode* Delete(struct ListNode* head, int i) {
    struct ListNode* p1, * p2;
    p1 = p2 = head;
    while (p1!=NULL&&p1->num != i) {
        p2 = p1;
        p1 = p1->next;
    }
    if (p1 == head) {
        head = head->next;
    }
    else {
        p2->next = p1->next;
    }
    return head;
}
int main() {
    struct ListNode* head=NULL;
    int a, b;
    head=create(head);
    print(head);
    printf("請(qǐng)輸入插入位置:");
    scanf("%d", &a);
    head = insert(head,a);
    print(head);
    printf("請(qǐng)輸入刪除值:");
    scanf("%d", &b);
    head = Delete(head, b);//刪除數(shù)據(jù)
    print(head);
	return 0;
}

效果如下:

因此,我們便實(shí)現(xiàn)了對(duì)單鏈表的創(chuàng)建、刪除、增加和輸出

總結(jié)

到此這篇關(guān)于C語言數(shù)據(jù)結(jié)構(gòu)之順序表和單鏈表的文章就介紹到這了,更多相關(guān)C語言順序表和單鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++實(shí)現(xiàn)類似延時(shí)停頓的打字效果

    C++實(shí)現(xiàn)類似延時(shí)停頓的打字效果

    這篇文章主要介紹的是使用C++實(shí)現(xiàn)類似延時(shí)停頓的打字效果的代碼,非常的簡(jiǎn)單,推薦給大家,有需要的小伙伴可以參考下。
    2015-03-03
  • 減少C++代碼編譯時(shí)間的簡(jiǎn)單方法(必看篇)

    減少C++代碼編譯時(shí)間的簡(jiǎn)單方法(必看篇)

    下面小編就為大家?guī)硪黄獪p少C++代碼編譯時(shí)間的簡(jiǎn)單方法(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • Linux下C語言的fork()子進(jìn)程函數(shù)用法及相關(guān)問題解析

    Linux下C語言的fork()子進(jìn)程函數(shù)用法及相關(guān)問題解析

    fork()函數(shù)在Linux下可以用于產(chǎn)生一個(gè)子進(jìn)程,這里我們挑選了兩個(gè)fork相關(guān)的面試題,來看一下Linux下C語言的fork()子進(jìn)程函數(shù)用法及相關(guān)問題解析
    2016-06-06
  • C++11/C++14中constexpr的使用案例詳解

    C++11/C++14中constexpr的使用案例詳解

    C++11規(guī)定,允許將變量聲明為constexpr類型以便由編譯器來驗(yàn)證變量的值是否是一個(gè)常量表達(dá)式,這篇文章主要介紹了C++11/C++14中constexpr的使用,需要的朋友可以參考下
    2023-06-06
  • 關(guān)于c++編譯protobuf時(shí)提示LNK2001 無法解析的外部符號(hào)的問題

    關(guān)于c++編譯protobuf時(shí)提示LNK2001 無法解析的外部符號(hào)的問題

    這篇文章主要介紹了關(guān)于c++編譯protobuf時(shí)提示LNK2001 無法解析的外部符號(hào)的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • C語言多種方法實(shí)現(xiàn)一個(gè)函數(shù)左旋字符串中K個(gè)字符

    C語言多種方法實(shí)現(xiàn)一個(gè)函數(shù)左旋字符串中K個(gè)字符

    這篇文章主要為大家介紹了C語言多種方法實(shí)現(xiàn)一個(gè)函數(shù),可以左旋字符串中K個(gè)字符,文中附含詳細(xì)的示例講解,有需要的朋友可以借鑒參考下
    2021-10-10
  • c++ 指針與引用的區(qū)別介紹及使用說明

    c++ 指針與引用的區(qū)別介紹及使用說明

    指針與引用看上去完全不同(指針用操作符*和->,引用使用操作符.),但是它們似乎有相同的功能,感興趣的朋友可以了解下啊,或許本文對(duì)你有所幫助,好了,話不多說,切入正題
    2013-01-01
  • C語言實(shí)現(xiàn)像素鳥游戲

    C語言實(shí)現(xiàn)像素鳥游戲

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)像素鳥游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • C++容器vector實(shí)現(xiàn)通訊錄功能

    C++容器vector實(shí)現(xiàn)通訊錄功能

    這篇文章主要為大家詳細(xì)介紹了C++容器vector實(shí)現(xiàn)通訊錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • C語言一看就懂的指針與結(jié)構(gòu)體介紹

    C語言一看就懂的指針與結(jié)構(gòu)體介紹

    指針提供了對(duì)地址操作的一種方法,因此,使用指針可使得C語言能夠更高效地實(shí)現(xiàn)對(duì)計(jì)算機(jī)底層硬件的操作。另外,通過指針可以更便捷地操作數(shù)組。C數(shù)組允許定義可存儲(chǔ)相同類型數(shù)據(jù)項(xiàng)的變量,結(jié)構(gòu)是C編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許您存儲(chǔ)不同類型的數(shù)據(jù)項(xiàng)
    2022-04-04

最新評(píng)論