题解:后续的功能会不定期更新
emmm框架:
代码:
#include <iostream> #include<cstdio> using namespace std; typedef struct { int data; }ElemType; typedef struct LNode { ElemType elem; struct LNode *next; }LNode,*LinkList; int GetElem_L(LinkList L, int i, ElemType& e) { LNode *p = L->next; int j = 1; while (p&&j < i) { p = p->next; ++j; } if (!p || j>i) return 0; e = p->elem; return 1; } int ListInsert_L(LinkList &L, int i, ElemType e) { LNode *p = L->next; int j = 1; while (p && j < i-1) { p = p->next; ++j; } if (!p || j>i-1) return 0; LNode *node =(LNode*)malloc(sizeof(LNode)); node->elem = e; LNode* q = p->next; p->next = node; node->next = q; return 1; } int ListDelete_L(LinkList &L, int i, ElemType &e) { LNode *p = L->next; int j = 1; while (p->next&&j < i - 1)//注意此处为p->next,因为若是p,则出来的p可能为空 { p = p->next; ++j; } if (!p->next || j>i - 1) return 0; LNode*q= p->next; e = q->elem; p->next = p->next->next; free(q); return 1; } void CreateList_L(LinkList &L, int n) { printf("Enter the value of the node:"); // L = (LinkList)malloc(n*sizeof(LNode)); 如果像这样创建的话, //那就是生成连续存储空间的线性表,应该单独对每一个节点分配内存空间 L = (LinkList)malloc(sizeof(LNode)); L->next = nullptr;//先生成一个表头的单链表 for (int i = n;i > 0;--i) { LNode *pnode = (LinkList)malloc(sizeof(LNode)); scanf("%d",&pnode->elem.data); pnode->next = L->next;//插入方式为向表头的后一个插入,不然插在表尾太麻烦 L->next = pnode; } } void ShowList(LinkList &L) { LNode *pNode = L->next; while (pNode) { printf("%d ", pNode->elem.data); pNode = pNode->next; } } int main() { LinkList L; cout<<"Enter the length of the linked list:"; int num; cin>>num; CreateList_L(L, num);//建表 ShowList(L);//展示 ElemType e1,temp; cout<<"\nTo increase the number of positions:"; int place; cin>>place; cout<<"\nEnter the number to insert:"; cin>>e1.data; ListInsert_L(L, place, e1); ShowList(L); cout<<"\nThe number of digits to be deleted:"; int place1; cin>>place1; ListDelete_L(L, place1, temp); ShowList(L); printf("\nThe deleted node value is :%d\n", temp.data); return 0; }
今天也是元气满满的一天!good luck!
时间: 2024-12-12 20:37:42