各位高手救救我吧!!!
求助:关于双向链表
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define OK 1
#define ERROR 0
typedef struct DLNode{
int data;
struct DLNode *prior,*next;
}DLNode,*DLinkList;
int ListInsert(DLinkList,int,int);
int ListDelete(DLinkList,int,int*);
DLinkList GetElem(DLinkList,int);
main()
{
DLinkList head;
head->next=head->prior=head;
ListInsert(head,1,3);
getch ();
return 0;
}
int ListInsert(DLinkList l,int i,int e)
{
DLinkList p,s;
if(!(p=GetElem(l,i)))
return ERROR;
if(!(s=(DLinkList)malloc(sizeof(DLNode))))
return ERROR;
s->data=e;
s->prior=p->prior;
p->prior->next=s;
s->next=p;
p->prior=s;
return OK;
}
DLinkList GetElem(DLinkList l,int i)
{
DLinkList p;
int j=0;
p=l->next;
while(p&&j<i){
p=p->next;
++j;
}
if(!p || j>i)
return ERROR;
return p;
}
int ListDelete(DLinkList l,int i,int *e)
{
DLinkList p;
if(!(p=GetElem(l,i)))
return ERROR;
*e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
return OK;
}
这是一个双向链表的小程序;
为什么编译就出现"该内存不能为"written"";
#include<stdlib.h>
#include<conio.h>
#define OK 1
#define ERROR 0
typedef struct DLNode{
int data;
struct DLNode *prior,*next;
}DLNode,*DLinkList;
int ListInsert(DLinkList,int,int);
int ListDelete(DLinkList,int,int*);
DLinkList GetElem(DLinkList,int);
main()
{
DLinkList head;
head->next=head->prior=head;
ListInsert(head,1,3);
getch ();
return 0;
}
int ListInsert(DLinkList l,int i,int e)
{
DLinkList p,s;
if(!(p=GetElem(l,i)))
return ERROR;
if(!(s=(DLinkList)malloc(sizeof(DLNode))))
return ERROR;
s->data=e;
s->prior=p->prior;
p->prior->next=s;
s->next=p;
p->prior=s;
return OK;
}
DLinkList GetElem(DLinkList l,int i)
{
DLinkList p;
int j=0;
p=l->next;
while(p&&j<i){
p=p->next;
++j;
}
if(!p || j>i)
return ERROR;
return p;
}
int ListDelete(DLinkList l,int i,int *e)
{
DLinkList p;
if(!(p=GetElem(l,i)))
return ERROR;
*e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
return OK;
}
这是一个双向链表的小程序;
为什么编译就出现"该内存不能为"written"";