#include <stdio.h> #include <stdlib.h> #define ElementType int typedef struct LNode *PtrToNode; struct LNode { ElementType Data; PtrToNode Next; }; typedef PtrToNode List; void InitList(List &PtrL){ PtrL = (List)malloc(sizeof(struct LNode)); PtrL->Next=NULL; return; } bool Insert(ElementType X, int i,List PtrL){ List Tmp,Pre=PtrL; int j = 1; while(j<i&&Pre){ j++; Pre = Pre->Next; } if(Pre==NULL){ printf("插入位置不合理 "); return false; }else{ Tmp = (List)malloc(sizeof(struct LNode)); Tmp->Data =X; Tmp->Next = Pre->Next; Pre->Next=Tmp; return true; } } bool Delete(int i, List &PtrL){ List Tmp,Pre=PtrL; int j = 1; while(j<i&&Pre){ j++; Pre = Pre->Next; } if(Pre==NULL||Pre->Next==NULL){ printf("插入位置不合理 "); return false; }else{ Tmp = Pre->Next; Pre->Next = Tmp->Next; free(Tmp); return true; } } void printL(List PtrL){ List TmpList = PtrL->Next; while(TmpList){ printf("%d ",TmpList->Data); TmpList = TmpList->Next; } } int main(){ List PtrL; InitList(PtrL); for(int i = 1;i<10;i++) Insert(i,i,PtrL); printL(PtrL); for (int i = 12;i>5;i--) Delete(i,PtrL); printL(PtrL); }