写了个双向链表,有错,不知道原因
一共有两处错误
/* DNodeStr.txt */
#include <stdio.h>
#define ElemType char
struct DNode
{
ElemType data;
struct DNode *right, *left;
};
/* DNodeMut.txt */ /* error message: compound statement missing } in function insert */
#include "DNodeStr.txt"
setnull (struct DNode **p)
{
*p=NULL;
}
int length (struct DNode **p)
{
int n=0;
struct DNode *q;
q=*p;
while (q!=NULL)
{
n++;
q=q->right;
}
return (n);
}
ElemType get (struct DNode **p, int i)
{
int j;
struct DNode *q;
q=*p;
for (j=1; j<i && q!=NULL; j++)
q=q->right;
if (q!=NULL)
return (q->data);
else
printf ("wrong location number!\n");
}
int locate (struct DNode **p, ElemType x)
{
int j=1;
struct DNode *q;
q=*p;
while (q!=NULL && q->data!=x)
{
q=q->right;
j++;
}
if (q!=NULL)
return (j);
else
return (-1);
}
void insert (struct DNode **p, ElemType x, int i)
{
int j;
struct DNode *s,*q;
q=*p;
s=(struct DNode *) malloc (sizeof (struct DNode));
s->data=x;
s->left=NULL;
s->right=NULL;
if (i==1)
{
s->right=q;
q->left=s;
*p=s;
}
else
{
for (j=1; j<i-1 && q->right!=NULL; j++)
q=q->right;
if (j==i-1)
{
if (q->right!=NULL)
{
s->right=q->right;
q->right->left=s;
q->right=s;
s->left=q;
}
else
{
q->right=s;
s->left=q;
}
}
else
printf("wrong location number!\n");
}
void del (struct DNode **p, int i) /* error message: expression syntax in function insert */
{
struct DNode *q,*t;
q=*p;
t=q;
if (i==1)
{
q=q->right;
if (q!=NULL)
{
q->left=NULL;
*p=q;
}
}
else
{
for (int j=1; j<i-1 && q->right!=NULL; j++)
q=q->right;
if (j=i-1)
{
if (q->right->right!=NULL)
{
t=q;
q->right=t->right->right;
q->right->left=t;
}
else
q->right=NULL;
}
else
printf("wrong location number!\n");
}
}
void display(struct DNode **p)
{
struct DNode *q;
q=*p;
printf ("display the Double Node table: \n");
if (q==NULL)
printf ("Empty Double Node table.\n");
else if (q->right==NULL)
printf ("%c\n ", q->data);
else
{
while (q->right!=NULL)
{
printf ("%c->", q->data);
q=q->right;
}
printf ("%c \n", q->data)
}
}
/* DNodeMai.txt */
#include "DNodeMut.txt"
int main()
{
struct DNode *head;
setnull (&head);
insert (&head, 'a', 1);
insert (&head, 'b', 2);
insert (&head, 'c', 2);
insert (&head, 'd', 1);
display (&head); /*the output should be: dacb */
printf("The length of the LNode is: %d \n", length (&head));
printf("The location of the elem a is: %d \n", locate(&head, 'a'));
printf("The value of the location 3 is: %c \n", get (&head, 3));
del (&head, 2);
display (&head); /*the output should be: dcb */
printf("The length of the LNode is: %d \n", length (&head));
}