C語言數據結構之順序表和單鏈表

一、順序表的創建、刪除和插入

#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;//創建順序表L
	InitList(L);//初始化順序表
	shuru(L);//輸入值
	ListInsert(L, 3, 3);//插入值
	print(L);//打印
	ListDelete(L, 3, 3);//刪除值
	print(L);
	return 0;
}

以上操作分別實現瞭對順序表的創建,插入,刪除和打印

二、單鏈表的創建、刪除、增加和輸出

#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);//創建鏈表
    print(head);//輸出鏈表
	return 0;
}

以上操作為創建鏈表並打印,效果如下:

現在增加插入操作

#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("請輸入插入的數:");
    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("請輸入插入位置:");
    scanf("%d", &a);
    head = insert(head,a);//插入新數據
    print(head);
	return 0;
}

效果如下:

現增加刪除操作

#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("請輸入插入的數:");
    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("請輸入插入位置:");
    scanf("%d", &a);
    head = insert(head,a);
    print(head);
    printf("請輸入刪除值:");
    scanf("%d", &b);
    head = Delete(head, b);//刪除數據
    print(head);
	return 0;
}

效果如下:

因此,我們便實現瞭對單鏈表的創建、刪除、增加和輸出

總結

到此這篇關於C語言數據結構之順序表和單鏈表的文章就介紹到這瞭,更多相關C語言順序表和單鏈表內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: