C语言之顺序表&单链表
Posted Ja_king_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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数据结构单链表接口函数逻辑解析与代码实现(含详细代码注释)