程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言問答 >> c語言雙向鏈

c語言雙向鏈

編輯:C語言問答

c語言雙向鏈

5

#include"stdio.h"
#include"malloc.h"
#define NULL 0
struct node
{
    char name[20];
    struct node *next,*prior;
};
struct node *creat(int n)
{
    struct node *h,*p,*s;
    int i;
    h=(struct node*)malloc(sizeof(struct node));
    h->name[0]='\0';
    h->prior=NULL; /*初始化?*/
    h->next=NULL;
    p=h;
    for(i=0;i<n;i++)
    {
        s=(struct node*)malloc(sizeof(struct node));
        p->next=s;
        printf("Input the %d student's name:",i+1);
        scanf("%s",s->name);
        s->prior=p;/*這下面的三排是干什麼的*/
        s->next=NULL;
        p=s;
    }
    p->next=NULL;
    return h;
}
struct node *search(struct node *h,char*x)
{
    struct node *p;
    char *y;
    p=h->next;
    while(p)
    {
        y=p->name;
        if(strcmp(y,x)==0)return p;
        else p=p->next;
    }
    printf("cannot find datd!\n");
}
void del(struct node *p)
{   /*這下面的三行是干什麼的?*/
    p->next->prior=p->prior;
    p->prior->next=p->next;
    free(p);
}
main()
{
    int number;
    char sname[20];
    struct node *head,*sp;
    puts("Please in put the size of the list;");
    scanf("%d",&number);
    head=creat(number);
    sp=head->next;
    printf("\nNow the double list is:\n");
    while(sp)
    {
        printf("  %s",&*(sp->name));
        sp=sp->next;
    }
    printf("\nPlease input the name which you want to find:\n");
    scanf("%s",sname);
    sp=search(head,sname);
    printf("the name you want to find is: %s\n",*&sp->name);
    del(sp);
    sp=head->next;
    printf("\nNow the double list is:\n");
    while(sp)
    {
        printf("  %s",&*(sp->name));
        sp=sp->next;
    }
    printf("\n");
    puts("\n Press any key to quit...");
    getch();
}求人解釋我備注了的地方

最佳回答:

#include"stdio.h"
#include"malloc.h"
#define NULL 0
struct node
{
    char name[20];
    struct node *next,*prior;  //定義結構結點指針
};
struct node *creat(int n)
{
    struct node *h,*p,*s;
    int i;
    h=(struct node*)malloc(sizeof(struct node));
    h->name[0]='\0';
    h->prior=NULL; /*此處相當於建立一個空節點h,初始化雙向鏈表頭結點*/
    h->next=NULL;
    p=h;
    for(i=0;i<n;i++)
    {
        s=(struct node*)malloc(sizeof(struct node));
        p->next=s; // 讓指針p所指結點指向s結點,即h結點的下一結點是s
        printf("Input the %d student's name:",i+1);
        scanf("%s",s->name);
        s->prior=p;/*結點s與指針p所指結點建立連接,s的前一節點是指針p所指結點*/
        s->next=NULL; //s指向空節點,即s是尾結點
        p=s; //讓指針p指向向結點s,此時指針p所指結點編出s
    }
    p->next=NULL;
    return h;
}
struct node *search(struct node *h,char*x)
{
    struct node *p;
    char *y;
    p=h->next;
    while(p)
    {
        y=p->name;
        if(strcmp(y,x)==0)return p;
        else p=p->next;
    }
    printf("cannot find datd!\n");
}
void del(struct node *p)
{   /*刪除指針p指向的結點*/
    p->next->prior=p->prior;//指針p所指結點的下一結點的頭結點,指向指針p所指結點的前一節點

    p->prior->next=p->next; // 指針p所指結點的前一節點指向,指針p所指結點的後一節點

//這兩步實現將p所指結點,排除到雙向鏈表之外
    free(p); //刪除p所指結點
}
main()
{
    int number;
    char sname[20];
    struct node *head,*sp;
    puts("Please in put the size of the list;");
    scanf("%d",&number);
    head=creat(number);
    sp=head->next;
    printf("\nNow the double list is:\n");
    while(sp)
    {
        printf("  %s",&*(sp->name));
        sp=sp->next;
    }
    printf("\nPlease input the name which you want to find:\n");
    scanf("%s",sname);
    sp=search(head,sname);
    printf("the name you want to find is: %s\n",*&sp->name);
    del(sp);
    sp=head->next;
    printf("\nNow the double list is:\n");
    while(sp)
    {
        printf("  %s",&*(sp->name));
        sp=sp->next;
    }
    printf("\n");
    puts("\n Press any key to quit...");
    getch();
}

追問:

p->next->prior這樣指是什麼怎麼指向兩個了好向

p->prior->next

回答:

這樣說吧,假如我令 指針p所指結點的前一個結點是k,後一個結點是q。

那麼三個結點依次是 k,p,q

p->next->prior 相當於指針p所指的結點的下一個結點(也就是上面所假設的q結點)的前指針,即q->prior ,結點q的前指針

p->prior->next 相當於指針p所指結點的前指針(也就是指結點k)的下一指針,即k->next,結點k的下一指針

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved