#include <stdio.h>
#include <malloc.h>
typedef int DataType;
typedef struct node{
DataType data;
struct node *next;
}LinkList;
void *InitList(LinkList *head);
LinkList *AddHead(LinkList *head,DataType x);
int ListLen(LinkList *head);
LinkList *GetNode(LinkList *head,int i);
LinkList *LocateNode(LinkList *head,int x);
void DeleList(LinkList *head,int i);
void main(){
LinkList *l=NULL,*p=NULL;
int x=0;
l = (LinkList *)InitList(l);
x = ListLen(l);
printf("%d\n",x);
AddHead(l,4);
x = ListLen(l);
printf("%d\n",x);
AddHead(l,2);
x = ListLen(l);
printf("%d\n",x);
DeleList(l,1);
x = ListLen(l);
p = GetNode(l,1);
printf("%d\n",p->data);
}
void *InitList(LinkList *head)
{
head=(LinkList*)malloc(sizeof(LinkList));
head->next=NULL;
return head;
}
LinkList *AddHead(LinkList *head,DataType x){
LinkList *p;
p = (LinkList *)malloc(sizeof(LinkList));
p->data = x;
p->next = head->next; head->next = p;
return head;
}
int ListLen(LinkList *head){
int len = 0;
LinkList *p = head;
while(p->next!=NULL){
len++;
p=p->next;
}
return len;
}
LinkList *GetNode(LinkList *head,int i){
LinkList *p = head;
int j = 0;
while(p->next&&j<i){
j++;
p=p->next;
}
if(j==i) return p;
else return NULL;
}
LinkList *LocateNode(LinkList *head,int x){
LinkList *p=head->next;
while(p&&p->data!=x)
p=p->next;
return p;
}
void DeleList(LinkList *head,int i){
LinkList *p = head,*q = NULL;
int j = 0;
while(p->next&&j<i){
j++;
q=p;
p=p->next;
}
if(j==i){
q->next = p->next;
free(q);
printf("删除成功!");
}
else printf("超出链表范围!");
}
好像也是上次类似的错误但是我调试不好
第一次用VC编东西感觉还是很怪,
不好意思了,还得向你请教。