单链表问题,帮忙改下
程序代码:
#include <stdio.h> #include <malloc.h> //单链表定义 typedef struct LNode { int data; struct LNode *next; }LNode, *LinkList; //单链表初始化 int InitLinkList(LinkList &L) { L = (LinkList)malloc(sizeof(LNode)); if(!L) return -1; L -> next = NULL; return 1; } //单链表求长 int listLength(LinkList L) { LNode *p = L; int j = 0; while(p -> next) { p = p -> next; j++; } return j; } //单链表取元素 int getElem(LinkList L, int i, int *e) { LNode *p = L; int j = 0; while(j < i && p -> next) { p = p -> next; j++; } if(j == i) { e = p -> data; return 1; } else return -1; } //单链表查找 LinkList locateElem(LinkList L, int e) { LNode *p = L -> next; while(p && (p -> data != e)) p = p -> next; if(p) return p; else return NULL; } //单链表插入 int listInsert(LinkList *L, int i, int e) { LNode *p = L, *q; int j = 0; while(j < i - 1 && p -> next) { p = p -> next; j++; } if(j == i - 1) { q = (LNode *)malloc(sizeof(LNode)); if(!q) return -1; q -> data = e; q -> next = p -> next; p -> next = q; return 1; } else return -1; } //单链表删除 int listDelete(LinkList *L, int i, int *e) { LNode *p = L, *q; int j = 0; while(j < i - 1 && p -> next) { p = p -> next; j++; } if(j == i - 1 && p -> next) { q = p -> next; p -> next = q -> next; e = q -> data; free(q); } else return -1; } int main() { LinkList L; return 0; }