五、实验内容和步骤 1.设计程序建立一个顺序表,要求从键盘输入整数,并完成添加插入和删除元素功能,并将该顺序表的元素从屏幕显示出来。 2.设计程序创建一个单向链表,并输出链表中数据。要求:从键盘输入元素的值,并在此基础上实现元素插入、删除及按值查找的功能。要求用c语言实现
时间: 2024-02-13 15:06:39 浏览: 44
好的,以下是建立顺序表和单向链表的代码实现。顺序表的添加插入和删除元素功能以及显示元素的功能已经实现。单向链表的创建以及插入、删除和按值查找元素的功能也已经实现。请注意,以下代码仅供参考。
建立顺序表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SeqList;
// 初始化顺序表
void InitList(SeqList *L) {
L->length = 0;
}
// 向顺序表中添加元素
void AddElement(SeqList *L, int x) {
if (L->length == MAXSIZE) {
printf("The list is full.\n");
return;
}
L->data[L->length] = x;
L->length++;
}
// 向顺序表中插入元素
void InsertElement(SeqList *L, int x, int pos) {
if (pos < 1 || pos > L->length + 1) {
printf("Invalid position.\n");
return;
}
if (L->length == MAXSIZE) {
printf("The list is full.\n");
return;
}
for (int i = L->length - 1; i >= pos - 1; i--) {
L->data[i + 1] = L->data[i];
}
L->data[pos - 1] = x;
L->length++;
}
// 从顺序表中删除元素
void DeleteElement(SeqList *L, int pos) {
if (pos < 1 || pos > L->length) {
printf("Invalid position.\n");
return;
}
for (int i = pos; i < L->length; i++) {
L->data[i - 1] = L->data[i];
}
L->length--;
}
// 显示顺序表中的元素
void DisplayList(SeqList *L) {
for (int i = 0; i < L->length; i++) {
printf("%d ", L->data[i]);
}
printf("\n");
}
int main() {
SeqList list;
InitList(&list);
int x, pos;
char op;
while (1) {
printf("Please enter an operation: ");
scanf(" %c", &op);
switch (op) {
case 'a':
printf("Please enter an element to add: ");
scanf("%d", &x);
AddElement(&list, x);
break;
case 'i':
printf("Please enter an element and a position to insert: ");
scanf("%d %d", &x, &pos);
InsertElement(&list, x, pos);
break;
case 'd':
printf("Please enter a position to delete: ");
scanf("%d", &pos);
DeleteElement(&list, pos);
break;
case 's':
printf("The elements in the list are: ");
DisplayList(&list);
break;
case 'q':
return 0;
default:
printf("Invalid operation.\n");
break;
}
}
}
```
创建单向链表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
// 创建单向链表
Node* CreateList() {
Node *head = NULL, *tail = NULL;
int x;
while (1) {
printf("Please enter an element (enter -1 to stop): ");
scanf("%d", &x);
if (x == -1) {
break;
}
Node *p = (Node*)malloc(sizeof(Node));
p->data = x;
p->next = NULL;
if (head == NULL) {
head = p;
tail = p;
} else {
tail->next = p;
tail = p;
}
}
return head;
}
// 向单向链表中插入元素
void InsertElement(Node **head, int x, int pos) {
Node *p = (Node*)malloc(sizeof(Node));
p->data = x;
if (pos == 1) {
p->next = *head;
*head = p;
return;
}
Node *q = *head;
for (int i = 1; i < pos - 1; i++) {
if (q == NULL) {
printf("Invalid position.\n");
return;
}
q = q->next;
}
if (q == NULL) {
printf("Invalid position.\n");
return;
}
p->next = q->next;
q->next = p;
}
// 从单向链表中删除元素
void DeleteElement(Node **head, int pos) {
if (*head == NULL) {
printf("The list is empty.\n");
return;
}
Node *p = *head;
if (pos == 1) {
*head = (*head)->next;
free(p);
return;
}
Node *q = *head;
for (int i = 1; i < pos - 1; i++) {
if (q == NULL) {
printf("Invalid position.\n");
return;
}
q = q->next;
}
if (q == NULL || q->next == NULL) {
printf("Invalid position.\n");
return;
}
p = q->next;
q->next = p->next;
free(p);
}
// 按值查找单向链表中的元素
int FindElement(Node *head, int x) {
Node *p = head;
int pos = 1;
while (p != NULL) {
if (p->data == x) {
return pos;
}
p = p->next;
pos++;
}
return -1;
}
// 显示单向链表中的元素
void DisplayList(Node *head) {
Node *p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
Node *head = CreateList();
int x, pos;
char op;
while (1) {
printf("Please enter an operation: ");
scanf(" %c", &op);
switch (op) {
case 'i':
printf("Please enter an element and a position to insert: ");
scanf("%d %d", &x, &pos);
InsertElement(&head, x, pos);
break;
case 'd':
printf("Please enter a position to delete: ");
scanf("%d", &pos);
DeleteElement(&head, pos);
break;
case 'f':
printf("Please enter an element to find: ");
scanf("%d", &x);
pos = FindElement(head, x);
if (pos == -1) {
printf("The element is not in the list.\n");
} else {
printf("The element is at position %d.\n", pos);
}
break;
case 's':
printf("The elements in the list are: ");
DisplayList(head);
break;
case 'q':
return 0;
default:
printf("Invalid operation.\n");
break;
}
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)