#include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> typedef struct node{ int data; struct node *next; }node; typedef struct node *LinkList; int GetElem(LinkList L,int i,int *e){ //获取元素 int j; LinkList p; p = L->next; j = 1; while(p && j<i){ p = p->next; ++j; } if(!p||j>i) return 0; *e = p->data; return 1; } int ListInsert(LinkList *L,int i,int e){ //插入操作 int j; LinkList p,s; p = *L; j = 1; while(p && j<i){ p = p->next; ++j; } if(!p || j>i) return 0; s = (LinkList)malloc(sizeof(node)); s->data = e; s->next = p->next; p->next = s; return 1; } int ListDelete(LinkList *L,int i,int *e){ //删除操作 int j; LinkList p,q; p = *L; j = 1; while(p->next && j<i){ p = p->next; ++j; } if(!(p->next)||j>i) return 0; q = p->next; p->next = q->next; *e = q->data; free(q); return 1; } void CreateListHead(LinkList *L,int n){ //头插入法 LinkList p; int i; srand(time(0)); *L = (LinkList)malloc(sizeof(node)); (*L)->next = NULL; for(i=0;i<n;i++){ p = (LinkList)malloc(sizeof(node)); p->data = rand()%100+1; p->next = (*L)->next; (*L)->next = p; } } void CreateListTail(LinkList *L,int n){ //尾插入法 LinkList p,r; int i; *L = (LinkList)malloc(sizeof(node)); r = *L; for(i=0;i<n;i++){ p = (LinkList)malloc(sizeof(node)); p->data = i+1; r->next = p; r = p; } r->next = NULL; } void ClearLisk(LinkList *L){ LinkList p,q; p = (*L)->next; while(p){ q = p->next; free(p); p = q; } (*L)->next = NULL; } int main(){ return 0; }
时间: 2024-11-05 14:39:10