请输入n个元素,-1结束:
1
2
3
4
100
-1
顺序表:
1 2 3 4 100
指定一个元素,在此元素之前插入一个新元素:5
5在表的[-1]下标位置
.请输入一个新元素值:11
插入位置无效
顺序表:
1 2 3 4 100
请输入列表中要删除的元素:4
删除元素后的顺序表:
1 2 3 100
- #define _CRT_SECURE_NO_WARNINGS
- #include
-
- #define MAX_SIZE 100 // 定义顺序表的最大容量
-
- // 定义顺序表结构
- typedef struct {
- int data[MAX_SIZE]; // 用于存储数据的数组
- int length; // 当前顺序表的长度
- } SeqList;
-
- // 初始化顺序表
- void init(SeqList* list) {
- list->length = 0;
- }
-
- // 插入元素位置从0开始算
- bool insert(SeqList* list, int position, int element) {
- if (list->length >= MAX_SIZE) {
- printf("顺序表已满,无法插入元素\n");
- return false;
- }
-
- if (position < 0 || position > list->length) {
- printf("插入位置无效\n");
- return false;
- }
-
- // 将插入位置后的元素依次后移
- for (int i = list->length - 1; i >= position; i--) {
- list->data[i + 1] = list->data[i];
- }
-
- // 插入元素
- list->data[position] = element;
- list->length++;
-
- return true;
- }
-
- // 删除元素
- bool removeElement(SeqList* list, int data) {
- if (list->length == 0) {
- printf("顺序表为空,无法删除元素\n");
- return false;
- }
-
- int position=-1;
- for (int i = 0; i < list->length ; i++) {
- if (list->data[i] == data) {
- position = i;
- break;
- }
- }
- if (position == -1) {
- printf("没找到此元素,无法删除\n");
- return false;
- }
-
- // 将删除位置后的元素依次前移
- for (int i = position; i < list->length - 1; i++) {
- list->data[i] = list->data[i + 1];
- }
-
- list->length--;
-
- return true;
- }
-
- // 打印顺序表中的元素
- void printList(SeqList* list) {
- for (int i = 0; i < list->length; i++) {
- printf("%d ", list->data[i]);
- }
- printf("\n");
- }
-
- //找n的位置
- int find(SeqList* list, int n) {
- int position = -1;
- for(int i=0;i
length;i++) - if (list->data[i] == n) {
- position = i;
- break;
- }
- return position;
- }
-
- int main() {
- SeqList list; //list是变量
- int n;
- int res;
- init(&list);
- puts("请输入n个元素,-1结束:");
- int i = 0;
- while (1) {
- scanf("%d",&n);
- if (n == -1) {
- break;
- }
- else {
- insert(&list, i++, n);
- }
- }
- printf("顺序表:\n");
- printList(&list);
-
- printf("指定一个元素,在此元素之前插入一个新元素:");
- scanf("%d",&n);
- res = find(&list, n);
- printf("%d在表的[%d]下标位置\n.",n,res);
- printf("请输入一个新元素值:");
- scanf("%d", &n);
- insert(&list, res, n);
-
- printf("顺序表:\n");
- printList(&list);
-
-
- printf("请输入列表中要删除的元素:");
- scanf("%d", &n);
-
- res = removeElement(&list, n);
- if (res) {
- printf("删除元素后的顺序表:\n");
- printList(&list);
- }
-
-
- return 0;
- }
- #define _CRT_SECURE_NO_WARNINGS
- #include
- #include
-
- // 定义链表节点结构
- typedef struct Node {
- int data; // 节点数据
- struct Node* next; // 指向下一个节点的指针
- }Node_t,*List;
-
- // 初始化链表
- struct Node* initList() {
- return NULL; // 返回一个空链表
- }
-
- // 插入节点到链表头部
- void insertNode(List* head, int data, int position) {
- Node_t* newNode = (Node_t*)malloc(sizeof(Node_t)); // 创建新节点
- if (newNode == NULL) {
- printf("内存分配失败\n");
- return ;
- }
- newNode->data = data; // 设置节点数据
-
-
- if (position == 0 || head == NULL) {
- // 插入位置为链表头或链表为空
- newNode->next = *head;
- *head=newNode; // 新节点成为新的头节点
- return;
- }
-
- struct Node* current = *head;
- int index = 0;
- while (index < position - 1 && current->next != NULL) {
- current = current->next;
- index++;
- }
-
- newNode->next = current->next; // 新节点指向当前节点的下一个节点
- current->next = newNode; // 当前节点指向新节点
- }
-
- // 删除节点
- Node_t* deleteNode(List head, int data) {
- Node_t* current = head;
- Node_t* prev = NULL;
-
- while (current != NULL) {
- if (current->data == data) {
- if (prev == NULL) {
- // 删除头节点
- head = current->next;
- }
- else {
- // 删除非头节点
- prev->next = current->next;
- }
- free(current); // 释放删除的节点
- return head; // 返回新的头节点
- }
- prev = current;
- current = current->next;
- }
-
- printf("未找到要删除的节点\n");
- return head;
- }
-
- // 遍历并显示链表节点
- void displayList(List head) {
- Node_t* current = head;
- while (current != NULL) {
- printf("%d ", current->data);
- current = current->next;
- }
- printf("\n");
- }
-
- // 释放链表内存
- void freeList(List head) {
- struct Node* current = head;
- while (current != NULL) {
- struct Node* temp = current;
- current = current->next;
- free(temp); // 释放节点内存
- }
- }
- int find_n(List head, int n) {
- Node_t* current = head;
- int i = 0;
- while (current != NULL) {
- if (current->data == n)
- return i;
- current = current->next;
- i += 1;
- }
- return -1;
-
- }
- int main() {
- struct Node* myList = initList();
-
- puts("请输入n个元素,-1结束:");
- int n,res;
- int i = 0;
- while (1) {
- scanf("%d", &n);
- if (n == -1) {
- break;
- }
- else {
- insertNode(&myList, n,i++);
- }
- }
-
- printf("链表内容:\n");
- displayList(myList);
-
- printf("指定一个元素,在此元素之前插入一个新元素:");
- scanf("%d", &n);
- res = find_n(myList, n);
- printf("%d在表的[%d]下标位置\n", n, res);
- printf("请输入一个新元素值:");
- scanf("%d", &n);
- insertNode(&myList, n,res);
-
- printf("链表内容:\n");
- displayList(myList);
-
-
- printf("请输入链上要删的元素值:");
- scanf("%d", &n);
-
- myList = deleteNode(myList, n);
- printf("删除节点后的链表内容:\n");
- displayList(myList);
-
- freeList(myList); // 释放链表内存
-
- return 0;
- }