欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言数据结构之顺序表和单链表

程序员文章站 2022-03-17 15:28:57
一、顺序表的创建、删除和插入#define _crt_secure_no_warnings 1#includestruct sqlist {int date[10];in...

一、顺序表的创建、删除和插入

#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;
}

以上操作为创建链表并打印,效果如下:

C语言数据结构之顺序表和单链表

现在增加插入操作

#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;
}

效果如下:

C语言数据结构之顺序表和单链表

现增加删除操作

#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语言数据结构之顺序表和单链表的文章就介绍到这了,更多相关c语言顺序表和单链表内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!