#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define STR 45
typedef struct movie {
       char book_name[STR];
       int play;
       struct movie * left;
       struct movie * right;
       }DbNode,*pdbnode;
int main(void)
    {
    char input[STR];
    pdbnode head=NULL;
    pdbnode sc,yumen;
    puts("insert movie name:");
    while (gets(input)!=NULL&&input[0]!='\0'){
          sc=(pdbnode)malloc(sizeof(struct movie));
          if(head==NULL){
            head=sc;
     }else{
            yumen->right=sc;
     }
     sc->right=NULL;
     strcpy(sc->book_name,input);
     puts("shu ru why play:");
     scanf("%d",&sc->play);
     while(getchar()!='\n'){
          continue;
          }
     puts("right movie title");
    yumen=sc;

         }
if (head==NULL){
   printf("not data this!!");
}else{
   printf("have data");
}
//if (sc->right==NULL){
// sc->right=head;
//}

sc=head;
while(sc!=NULL){
     printf("movie title->%s,movie play->%d\n",sc->book_name,sc->play);
     sc=sc->left;
}
sc=head;
while(sc !=NULL){
     free(sc);
     sc=sc->right;
}
return 0;
}


为了让这个代码更加理解,把以上代码加上调试信息:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define STR 45
typedef struct movie
{
char book_name[STR];
int play;
struct movie *left;
struct movie *right;
} DbNode, *pdbnode;

int main(void)
    {
    char input[STR];
    pdbnode head=NULL;
    pdbnode sc,yumen;
    puts("insert movie name:");
    while (gets(input)!=NULL&&input[0]!='\0'){
          sc=(pdbnode)malloc(sizeof(struct movie));
          if(head==NULL){
            head=sc;
        }else{
            yumen->right=sc;
        }
     sc->right=NULL;
     strcpy(sc->book_name,input);
     puts("shu ru why play:");
     scanf("%d",&sc->play);
     while(getchar()!='\n'){
          continue;
          }
     puts("right movie title");

     printf("sc赋值之前---yumen=%p\n",yumen);
     printf("sc赋值之前---yumen->next=%p\n",yumen->right);
     yumen=sc;
     printf("sc赋值之后yumen->%p\n",yumen);
     printf("sc->%p\n",sc);
     printf("head->%p\n",head);
     printf("head->next=%p\n",head->right);
     }
if (head==NULL){
   printf("not data this!!");
}else{
   printf("have data");
}
//if (sc->right==NULL){
// sc->right=head;
//}
sc=head;
while(sc!=NULL){
     //printf("movie title->%s,movie play->%d\n",sc->book_name,sc->play);
    // printf("movie title->%p\n",sc);
    sc=sc->right;
}
sc=head;
while(sc !=NULL){
     free(sc);
     sc=sc->right;
}
return 0;
}

一下是运行出来的信息:


[root@localhost test]# ./a.out
insert movie name:
sdfs
shu ru why play:
1        ----------》第一次循环
right movie title
sc赋值之前---yumen=0x30e390 由于未赋值,系统随便指了个地址
sc赋值之前---yumen->next=0x89000000   由于未赋值,系统随便指了个地址
sc赋值之后yumen->0x879a008
sc->0x879a008
head->0x879a008
head->next=(nil) 三个指针指向的是同一个地址,由于是第一次循环,所以nextNULL
dfsf
shu ru why play:
2 ---------》第二次循环
right movie title
sc赋值之前---yumen=0x879a008
sc赋值之前---yumen->next=0x879a048 sc的新地址 相当于0x879a008-next=0x879a048
sc赋值之后yumen->0x879a048
sc->0x879a048
head->0x879a008
head->next=0x879a048
dsfs
shu ru why play:
3
right movie title
sc赋值之前---yumen=0x879a048
sc赋值之前---yumen->next=0x879a088 相当于0x879a008-next=0x879a048->next=0x879a088
sc赋值之后yumen->0x879a088
sc->0x879a088
head->0x879a008
head->next=0x879a048
dsfsf
shu ru why play:
4
right movie title
sc赋值之前---yumen=0x879a088
sc赋值之前---yumen->next=0x879a0c8 相当于0x879a008-next=0x879a048->next=0x879a088-next=0x879a0c8
sc赋值之后yumen->0x879a0c8
sc->0x879a0c8
head->0x879a008
head->next=0x879a048



用地址来说明,这样单向链表就清楚多了!!


双向链表:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define STR 45
typedef struct movie
{
char book_name[STR];
int play;
struct movie *left;
struct movie *right;
} DbNode, *pdbnode;


int main(void)
    {
    char input[STR];
    pdbnode head=NULL;
    pdbnode sc,yumen;
    puts("insert movie name:");
    while (gets(input)!=NULL&&input[0]!='\0'){
          sc=(pdbnode)malloc(sizeof(struct movie));
          if(head==NULL){
            head=sc;
        }else{
            yumen->right=sc;
        }
     strcpy(sc->book_name,input);
     puts("shu ru why play:");
     scanf("%d",&sc->play);
     while(getchar()!='\n'){
          continue;
          }
     puts("right movie titlei\n");
     sc->left=yumen; //每次循环把向前的指针指向前一个结构
     //printf("fu zhi zhi qian -->yumen:%p\n",yumen);
     //printf("sc->left=%p\n",sc->left);
     sc->right=NULL;
   yumen=sc;
     }
   head->left=sc;//循环结束之后,把头的向前指针指向尾
   yumen->right=head;//把尾的向后指针指向头

if (head==NULL){
   printf("not data this!!");
}else{
   printf("have data");
}
//if (sc->right==NULL){
// sc->right=head;
//}
pdbnode next;
next=head->left;
printf("head->book_name=%s,head->paly=%d\n",head->book_name,head->play);
while(next!=head){
    printf("movie title->%s,movie play->%d\n",next->book_name,next->play);
    next=next->left;
}
next=head->right;
while(next !=head){
     free(next);
     next=next->right;
}
return 0;
}