add SeqList

main
lwy 2 years ago
commit 2ad11f6cf8

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.24)
project(untitled1 C)
set(CMAKE_C_STANDARD 11)
add_executable(untitled1 main.c )

@ -0,0 +1 @@
C语言学习数据结构

BIN
SeqList/.DS_Store vendored

Binary file not shown.

@ -0,0 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int data;
struct ListNode *next;
} ListNode;
//typedef ListNode *LinkList;
//LinkList head;
//ListNode *p;
ListNode *initLink() {
ListNode *p = NULL;//创建头指针
ListNode *temp = (ListNode *) malloc(sizeof(ListNode));//创建首元节点
//首元节点先初始化
temp->data = 1;
temp->next = NULL;
p = temp;//头指针指向首元节点
//从第二个节点开始创建
for (int i = 2; i < 5; i++) {
//创建一个新节点并初始化
ListNode *a = (ListNode *) malloc(sizeof(ListNode));
a->data = i;
a->next = NULL;
//将temp节点与新建立的a节点建立逻辑关系
temp->next = a;
//指针temp每次都指向新链表的最后一个节点其实就是 a节点这里写temp=a也对
temp = temp->next;
}
//返回建立的节点,只返回头指针 p即可通过头指针即可找到整个链表
return p;
}
void display(ListNode *p) {
ListNode *temp = p;//将temp指针重新指向头结点
//只要temp指针指向的结点的next不是Null就执行输出语句。
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
ListNode *init() {
ListNode *p = NULL;
ListNode *temp = (ListNode *) malloc(sizeof(ListNode));
temp->data = 0;
temp->next = NULL;
p = temp;
for (int i = 1; i <= 5; i++) {
ListNode *a = (ListNode *) malloc(sizeof(ListNode));
a->data = i;
a->next = NULL;
temp->next = a;
temp = a;
}
return p;
}
ListNode *createList() {
//头插法
ListNode *p = NULL;
for (int i = 1; i <= 5; i++) {
ListNode *a = (ListNode *) malloc(sizeof(ListNode));
a->data = i;
a->next = p;
p = a;
}
return p;
}
ListNode *GetNodei(ListNode *head, int i) {
ListNode *p = head;
int j = 1;
while (p != NULL && j < i) {
p = p->next;
++j;
}
if (j == i) {
p->next = NULL;
return p;
} else {
return NULL;
}
}
ListNode *insertLinkList(ListNode *head, int i, int element) {
ListNode *p = head, *s;
int j = 0;
while (p != NULL && j < i) {
p = p->next;
++j;
}
if (p == NULL) {
printf("error\n");
return NULL;
} else {
s = (ListNode *) malloc(sizeof(ListNode));
s->data = element;
s->next = p->next;
p->next = s;
}
return head;
}

@ -0,0 +1,89 @@
#include <stdlib.h>
#include <stdio.h>
//顺序存储的线性表
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int length;
} SeqList;
void initList(SeqList *L);
void insertList(SeqList *L, int index, int element);
//初始化线性表
void initList(SeqList *L) {
L->length = 0;
}
//线性表插入元素
void insertList(SeqList *L, int index, int element) {
//在顺序表 L 中第 index 之前,插入新元素 element
int j;
if (index < 1 || index > L->length + 1) {
printf("Invalid index!\n");
return;
}
if (L->length >= MAX_SIZE) {
printf("List overflow!\n");
return;
}
//依次后移法
for (j = L->length - 1; j >= index - 1; j--) {
L->data[j + 1] = L->data[j];
}
L->data[index - 1] = element;
L->length++;
}
//线性表删除元素
int deleteList(SeqList *L, int index) {
if (index < 0 || index >= L->length) {
printf("Invalid index!\n");
exit(0);
}
int x = L->data[index - 1];
for (int j = index; j <= L->length; j++) {
L->data[j - 1] = L->data[j];
}
L->length--;
return x;
}
//获取线性表的长度
int listLength(SeqList L) {
return L.length;
}
//获取某个位置的元素
int getElem(SeqList *L, int i) {
if (i < 1 || i > L->length) {
return 0;
}
return L->data[i - 1];
}
//获取元素的位置
int locateElem(SeqList *L, int e) {
for (int i = 0; i < L->length; i++) {
if (L->data[i] == e) {
return i + 1;
}
}
return 0;
}
//遍历线性表
void readList(SeqList *L) {
if (L->length > 0) {
printf("线性表的长度是%d\n{", L->length);
for (int i = 0; i < L->length - 1; i++) {
printf("%d,", L->data[i]);
}
printf("%d}", L->data[L->length - 1]);
} else {
printf("List is empty!");
}
}

@ -0,0 +1,32 @@
#include "SeqList/SeqList.c"
#include "SeqList//LinkList.c"
int main() {
// SeqList s;
// initList(&s);
// insertList(&s, 1, 100);
// insertList(&s, 2, 200);
// insertList(&s, 3, 300);
// printf("%d\n",deleteList(&s,2));
// printf("%d", s.length);
// readList(&s);
// printf("%d",getElem(&s,1));
// printf("%d",locateElem(&s,400));
ListNode *l;
l = initLink();
display(l);
ListNode *k;
k = init();
display(k);
ListNode *m;
m = createList();
display(m);
ListNode *o;
// o = GetNodei(m, 3);
o = insertLinkList(m,3,100);
display(o);
}
Loading…
Cancel
Save