在2.9节,我写了一个明显错误的代码,是在双链表后初始化头节点后又初始化2个节点,并且分别命名为1,2后,删除第2个节点。然后重新显示这个链表中的每一个元素,我这个display是个死循环,但是也不该出现这种显示结果。
//循环单链表的初始化
//#include
//#include
//typedef struct LNode{
// int data;
// struct LNode *next;
//}LNode,*LinkList;
//bool InitList(LinkList &L){
// L=(LNode *)malloc(sizeof(LNode));
// if (L==NULL)
// return false;
// LNode *L1=(LNode *)malloc(sizeof(LNode));
// L->next=L1;
// L1->data=1;
// printf("%d\n",L1->data);
// printf("%s","哈哈哈");
// L1->next=L;
// return true;
//}
//void display(LinkList L){
// LNode *temp=(LNode *)malloc(sizeof(LNode));
// temp=L;
// while(temp){
// temp=temp->next;
//
// printf("%d\n",temp->data);
// }
//}
//bool isTail(LinkList L,LNode *p){
// if(p->next==L)
// return true;
// else
// return false;
//}
//bool Empty(LinkList L){
// if (L->next==L)
// return true;
// else
// return false;
//}
//int main(){
// LinkList L;
// InitList(L);
display(L);
// printf("%d\n",(isTail(L,L->next)));
// printf("%d",(Empty(L)));
//}
#include
#include
typedef struct LNode{
int data;
struct LNode *next,*prior;
}DNode, *DLinkList;
DLinkList Init(DLinkList &L){
L=(DNode *)malloc(sizeof(DNode));
DNode *L1=(DNode *)malloc(sizeof(DNode));
L->next=L1;
L->prior=NULL;
L->data=0;
printf("%d",L->data);
L1->next=L1;
L1->prior=L;
L1->data=1;
printf("%d",L1->data);
}
bool InsertNextDNode(DNode *p,DNode *s){
if(p==NULL)
return false;
// if (p->next==NULL)
s->next=p->next;
s->prior=p;
p->next->prior=s;
p->next=s;
printf("%d\n",s->data);
return true;
}
bool DeleteNextDNode(DLinkList &L,DNode *p){
if(p==NULL)
return false;
DNode *q=p->next;
if(p->next==L)
p->next=q->next;
q->next->prior=p;
free(q);
L=p->next;
}
void display(DLinkList L){
LNode *temp=(LNode *)malloc(sizeof(LNode));
temp=L;
while(temp){
temp=temp->next;
printf("%d\n",temp->data);
}
}
int main(){
DLinkList L;
Init(L);
DNode *s=(DNode *)malloc(sizeof(DNode));
s->data=2;
InsertNextDNode(L->next,s);
DeleteNextDNode(L,L->next);
display(L);
}
#include
#include
struct Node{
int data;
int next;
};
typedef struct{
int data;
int next;
}SLinkList[10];
int main(){
struct Node x;
printf("%d\n",sizeof(x));
struct Node a[10];
printf("%d\n",sizeof(a));
SLinkList b;
printf("%d",sizeof(b));
}
因为静态链表没有指针,所以初始化命名的时候也要注意。
这样关于结构体的重命名我好像会了,我又重新在2.7重命名了定义的单链表结构体,是可以执行的。
#include
#include
typedef struct LNode{
int data;
struct LNode *next;
//}LNode,*LinkList;
}Node,*LinkList;
LinkList List_Tailinsrt(LinkList&L){
int x;
// L=(LinkList)malloc(sizeof(LNode));
L=(LinkList)malloc(sizeof(Node));
LNode *r=L;
scanf("%d",&x);
while(x!=9999){
// LNode *s=(LNode*)malloc(sizeof(LNode));
Node *s=(Node*)malloc(sizeof(Node));
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return L;
}
void display(LinkList L){
LNode *temp=L;
while(temp){
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main(){
LinkList L;
List_Tailinsrt(L);
display(L);
}
进入第三章队列3.4
关于尾指针指向队尾元素的情况
只有这样初始化,Q.rear=(Q.rear+1)%Maxsize;Q.data[Q.rear]=x;才能用