顺序表的基本操作
1.用C语言数组实现顺序表,并在顺序表上实现如下操作
- 在第3个元素位置插入666
- 将第8个元素删除
- 在顺序表中查找值为65的元素,若存在则将其输出,否则打印没有该元素。
算法提示:在顺序表的某个位置插入元素,首先是要查看该位置是否合理,假如顺序表中有n个元素,要插入元素在第i个位置,那么插入的位置i应当在第1个与第n+1个元素位置之间,然后利用插入算法,将第n个元素一直到第i个元素依次后移,腾出一个空位,将新数据插入在该位置,最后将顺序表的表长加1.要删除顺序表中的元素,首先依然要判断要删除的元素是否在顺序表的合理位置,若是合理位置,则将包括元素在内的后面所有元素依次向前移动,直接用后继覆盖直接前驱,最后将顺序表的表长减1.查找某个元素是否在顺序表中,则可以利用顺序查找方法,从第一个元素开始依次将元素关键字的值与给定元素关键字的值进行比较,若相等,则返回该元素在顺序表中的位置,若不相等,则返回空
2.用C语言编程实现单链表的建立、插入、删除、按序号查找和按值查找算法
算法提示:单链表的建立是用头指针指向首结点,用另外一个指针(如P)指向尾结点,不断地生成新结点,再不断地将它们链接到单链表的尾部。单链表的插入是要生成一个要插入的结点,然后从头遍历单链表,确定要插入的位置,最后将要插入的结点插入到合理的位置。单链表的删除是将要删除结点前驱指向该结点的后继,然后将该结点空间释放。单链表的按序号查找是用一个计数器,从单链表第一个结点开始遍历,通过计数器确定要查找结点的位置,找到后再将它显示出来。单链表的按值查找也要从单链表的首部开始遍历,将单链表中的结点关键字值与给定结点的值进行比较,找到后将其显示。
1.
#include "stdafx.h"
#include "stdio.h"
#define MAX 30
// 全局变量
int a[MAX];
int n, i;
int b, x;
void insert(int, int);
void delete_i(int);
int locate(int);
void input_array();
void print_array();
void main() {
printf("Please input n: \n");
scanf("%d", &n);
printf("Please input a[1...%d]:\n", n);
input_array();
printf("Print array: a[1...%d]:\n", n);
print_array();
// 插入元素
i = 3;
b = 666;
printf("Insert a[%d]=%d result:\n", i, b);
insert(i, b);
print_array();
// 删除运算
i = 8;
printf("Delete a[%d]=%d result:\n", i, a[i]);
delete_i(i);
print_array();
// 查找运算
x = 65;
printf("Print array:\n");
print_array();
printf("Locate x=65, result:");
if (locate(x) == 0) {
printf("x does not exist.\n");
} else {
printf("Locate = %d\n", locate(x));
}
printf("Hello World\n");
return;
}
void insert(int i, int b) {
int j;
if ((i < 1) || (i > n + 1)) {
printf("Position does not exist.\n");
} else {
for (j = n; j >= i; j--) {
a[j] = a[j - 1];
}
a[i - 1] = b;
n++;
}
}
void delete_i(int i) {
int j;
if ((i < 1) || (i > n)) {
printf("Position does not exist.\n");
} else {
for (j = i; j < n; j++) {
a[j - 1] = a[j];
}
n--;
}
}
int locate(int x) {
int i = 1;
while ((i <= n) && (a[i - 1] != x)) {
i++;
}
if (a[i - 1] == x) {
return i;
} else {
return 0;
}
}
void input_array() {
for (i = 1; i <= n; i++) {
printf("a[%d] = ", i);
scanf("%d", &a[i - 1]);
}
}
void print_array() {
for (i = 1; i <= n; i++) {
printf("%d ", a[i - 1]);
}
printf("\n");
}
2.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
// 定义单链表节点结构体
typedef struct Node {
int data;
struct Node *next;
} Node;
// 建立单链表
Node* createList() {
Node *head, *p, *tail;
int x;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
tail = head;
printf("请输入节点数据,输入-1结束:\n");
scanf("%d", &x);
while (x!= -1) {
p = (Node*)malloc(sizeof(Node));
p->data = x;
tail->next = p;
tail = p;
scanf("%d", &x);
}
tail->next = NULL;
return head;
}
// 单链表插入操作
void insertNode(Node *head, int pos, int value) {
Node *p, *newNode;
int i;
p = head;
for (i = 0; i < pos && p!= NULL; i++) {
p = p->next;
}
if (p == NULL) {
printf("插入位置不合法\n");
return;
}
newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = p->next;
p->next = newNode;
}
// 单链表删除操作
void deleteNode(Node *head, int pos) {
Node *p, *q;
int i;
p = head;
for (i = 0; i < pos && p!= NULL; i++) {
p = p->next;
}
if (p == NULL || p->next == NULL) {
printf("删除位置不合法\n");
return;
}
q = p->next;
p->next = q->next;
free(q);
}
// 按序号查找
void searchByIndex(Node *head, int index) {
Node *p;
int i;
p = head->next;
for (i = 1; i < index && p!= NULL; i++) {
p = p->next;
}
if (p == NULL) {
printf("没有找到对应序号的节点\n");
} else {
printf("序号 %d 的节点值为:%d\n", index, p->data);
}
}
// 按值查找
void searchByValue(Node *head, int value) {
Node *p;
int index = 1;
p = head->next;
while (p!= NULL) {
if (p->data == value) {
printf("值为 %d 的节点序号为:%d\n", value, index);
return;
}
p = p->next;
index++;
}
printf("没有找到对应值的节点\n");
}
// 打印单链表
void printList(Node *head) {
Node *p;
p = head->next;
while (p!= NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
Node *head;
head = createList();
printf("建立的单链表为:");
printList(head);
insertNode(head, 2, 100);
printf("插入节点后的单链表为:");
printList(head);
deleteNode(head, 3);
printf("删除节点后的单链表为:");
printList(head);
searchByIndex(head, 4);
searchByValue(head, 100);
return 0;
}
3682




被折叠的 条评论
为什么被折叠?



