#include <iostream> #include <cstring> #include <string> #include <stdlib.h> using namespace std; int len = 0; typedef struct node { int data; node* next; }; void create_list(node*& p) { node* p1 = p, * s; scanf("%d", &len); for (int i = 0; i < len; i++) { s = (node*)malloc(sizeof(node)); cin >> s->data; s->next = p1->next; p1->next = s; } } void show_list(node*& n) { node* p; p = n->next; if (p == NULL) //链表为空 { cout << "Link list is empty\n"; } else { while (p != NULL) { cout << p->data << " "; p = p->next; } cout << "\n"; } } //删除指定位置的数 void delete_list(node *& n,int i) { node *p = n, *q; int j = 0; if (len == 0 || i > len) { printf("delete fail\n"); } else { while (j < i) //定位到要删除的位置 { q = p; p = p->next; j++; } q->next = p->next; free(p); len--; cout << "delete OK\n"; } } //获取第i个元素 void get_list(node*& n, int i) { node* p = n; int j = 0; if (len == 0 || i > len) { printf("get fail\n"); } else { while (j < i) { p = p->next; j++; } cout << p->data << "\n"; } } //插入node void insert_list(node*& n, int i, int e) { node* p = n, *s; int j = 0; if (i - 1 > len) { printf("insert fail\n"); } else { while (j < i - 1) { p = p->next; j++; } s = (node*)malloc(sizeof(node)); s->data = e; s->next = p->next; p->next = s; len++; cout << "insert OK\n"; } } int main() { int count, i, e; node* n = (node*)malloc(sizeof(node)); n->next = NULL; create_list(n); char flag[7]; scanf("%d", &count); scanf("%s", flag); while (count--) { if (!strcmp(flag,"show")) { show_list(n); } else if (!strcmp(flag, "delete")) { scanf("%d", &i); delete_list(n, i); } else if (!strcmp(flag, "insert")) { scanf("%d %d", &i, &e); insert_list(n, i, e); } else if (!strcmp(flag, "get")) { scanf("%d", &i); get_list(n, i); } scanf("%s", flag); } return 0; }
0.0分
3 人评分
C语言训练-大、小写问题 (C语言代码)浏览:719 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:497 |
快速排序算法1浏览:996 |
孤独的骑士 (C语言代码)浏览:1416 |
2004年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:627 |
纪念品分组 (C++代码)浏览:878 |
矩形面积交 (C语言代码)浏览:1879 |
母牛的故事 (C语言代码)浏览:3063 |
【绝对值排序】 (C语言代码)浏览:519 |
C语言训练-计算1977!* (C语言代码)浏览:959 |