#include <stdio.h>

#include <stdlib.h>


struct LNode {

int data;

int num;

struct LNode *next;

};


void dataShow(struct LNode *head);//输出函数


void charClear(char *a);//清空函数


void numClear(int *a);//清空函数


void charAnalyse(char *a, int *b); //解析函数


void nodeDelete(struct LNode **head, int loc, int *c); //删除函数


void insert(struct LNode **head, int loc, int *c, int a); //插入函数


void dataGet(struct LNode *head, int loc, int *c);


int main() {

int n;//结点个数

int m;//操作指令的行数

char str[50] = {'\0'};//存储字符串指令

int num[3] = {0};

int amount = 0; //记录结点个数

int count = 1;

struct LNode *head = NULL; //头指针

struct LNode *p0 = NULL;

struct LNode *p1 = NULL;

scanf("%d", &n);

head = (struct LNode *)malloc(sizeof(struct LNode));

p1 = head;

head->num = count++;

amount++;


for (int i = 0; i < n - 1; i++) {//创建链表


p0 = (struct LNode *)malloc(sizeof(struct LNode));

amount++;

p1->next = p0;

p0->num = count++;

p1 = p0;

}


p0->next = NULL;//此时p0指向链表的最后一个结点



for (int i = n; i >= 1; i--) { //将数据存入链表中


for (p1 = head; p1 != NULL; p1 = p1->next) {


if (p1->num == i) {

scanf("%d", &p1->data);

break;//一次只读一个,读到了就退出循环

}

}


}


scanf("%d", &m);


for (int i = 0; i <= m; i++) { //循环读取操作指令


gets(str);


if (str[0] == 's') {

dataShow(head);

charClear(str);


} else if (str[0] == 'd') {


charAnalyse(str, num);

nodeDelete(&head, num[0], &amount);

charClear(str);

numClear(num);

} else if (str[0] == 'i') {


charAnalyse(str, num);

insert(&head, num[0], &amount, num[1]);

charClear(str);

numClear(num);

} else if (str[0] == 'g') {

charAnalyse(str, num);

dataGet(head, num[0], &amount);

charClear(str);

numClear(num);

}

}


return 0;

}


void charClear(char *a) { //清空函数,清除字符数组中的遗留的脏数据

for (int i = 0; i < 50; i++) {


a[i] = '0';

}

}


void numClear(int *a) {//清空函数,清除整形数组中遗留的脏数据

for (int i = 0; i < 3; i++) {


a[i] = 0;

}

}


void charAnalyse(char *a, int *b) {//解析函数,将每行字符串指令中的数字解析出来

int temp = 0;

int count = 0;


for (int i = 0; i < 50; i++) {


if (a[i] >= '0' && a[i] <= '9') {

temp = temp * 10 + (a[i] - '0');

} else if (a[i] == ' ') {

if (temp > 0) {

b[count++] = temp;

temp = 0;

}

} else if (a[i] == '\0') {//因为字符串指令的最后无空格

if (temp > 0) {       //所以最后一个数无法存入数组

b[count++] = temp;//因此当遍历到最后一个位置时如果temp中还有有效值也要放入数组中

temp = 0;

}


break;

}

}

}


void dataShow(struct LNode *head) {//输出函数,输出所有结点数据

struct LNode *p0 = NULL;


if  (head->data == 0) {

printf("Link list is empty");

}


else {

for (p0 = head; p0 != NULL; p0 = p0->next) {


printf("%d ", p0->data);

}

}


printf("\n");

}


void nodeDelete(struct LNode **head, int loc, int *c) { //删除函数,删除指定结点

struct LNode *p0 = NULL;                             //删除结点要分是否为头结点进行讨论

struct LNode *p1 = NULL;

struct LNode *p2 = NULL;

p1 = *head;


if ((loc <= *c) && (p1->data != 0)) {

if ((loc == 1) && (*c > 1)) {

*head = (*head)->next;


for (p0 = *head; p0 != NULL; p0 = p0->next) {


p0->num--;

}


p1 = NULL;


} else if ((loc == 1) && (*c == 1)) {

p1->data = 0;

} else if (loc != 1) {

for (p0 = *head; p0 != NULL; p0 = p0->next) {


if (p0->num == loc) {

p1->next = p0->next;


for (p2 = p1->next; p2 != NULL; p2 = p2->next) {


p2->num--;

}


} else {

p1 = p0;

}

}


}


*c = *c - 1;

printf("delete OK\n");




} else if ((loc > *c) || (p1->data == 0)) {

printf("delete fail\n");

}

}


void insert(struct LNode **head, int loc, int *c, int a) {

struct LNode *p0 = NULL;

struct LNode *p1 = NULL;

struct LNode *p2 = NULL;

struct LNode *m = NULL;

struct LNode *temp = NULL;

p1 = *head;


if ((loc <= *c) || ((loc == 1) && (*c == 0))) {

for (p0 = *head; p0 != NULL; p0 = p0->next) {


if (p0->num == loc) {

if ((loc == 1) && (*c >= 1)) {

m = (struct LNode *)malloc(sizeof(struct LNode));

m->next = *head;

m->num = 1;

m->data = a;

temp = m;

m = *head;

*head = temp;


for (p2 = m; p2 != NULL; p2 = p2->next) {


p2->num++;

}

}


if (loc > 1) {

m = (struct LNode *)malloc(sizeof(struct LNode));

m->data = a;

m->num = p1->num + 1;

m->next = p1->next;

p1->next = m;


for (p2 = m->next; p2 != NULL; p2 = p2->next) {


p2->num++;

}

} else if ((loc == 1) && (*c == 0)) {

(*head)->data = a;

}


} else {

p1 = p0;

}

}


*c = *c + 1;

printf("insert OK\n");

} else {

printf("insert fail\n");

}

}


void dataGet(struct LNode *head, int loc, int *c) {

struct LNode *p0 = NULL;

int temp;


if (loc <= *c) {

for (p0 = head; p0 != NULL; p0 = p0->next) {


if (p0->num == loc) {

temp = p0->data;

break;

}

}


printf("%d\n", temp);

} else {

printf("get fail\n");

}

}


点赞(0)
 

0.0分

0 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论