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

Vanna ·
更新时间:2024-11-13
· 1513 次阅读

一、顺序表的创建、删除和插入 #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语言顺序表和单链表内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



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

需要 登录 后方可回复, 如果你还没有账号请 注册新账号