双链表 double_list

来源:互联网 发布:java聊天室设计与实现 编辑:程序博客网 时间:2024/06/08 19:10

.h 文件 double_node.h

//双链表 #define ElemType inttypedef struct Node * Pnode;//这里要加冒号 typedef struct Node{    int data;    struct Node *prev;      struct Node *next;} Node,* DoubleList;//函数声明 void  print_list(Node *pHead);DoubleList InitLink();void CreateList1(Pnode L,ElemType e);  //头插法建立双链表 void DeleteLink(Pnode L,int i);   //删除操作 Pnode double_find(Pnode l,  int num);  //查找第I 个结点 传进来头结点 

.c文件

//  双链表  #include "double_node.h" #include <stdio.h> #include <stdlib.h> int main() {    int n;    Node *pHead = NULL, *pFoot;    printf("双链表测试\n");    Pnode list=InitLink();   //链表初始化     CreateList1(list,56);    CreateList1(list,46);    print_list(list);    DeleteLink(list,2);     return 0; } //双链表初始化  //初始化链表 DoubleList InitLink(){    Pnode L =(Node *)malloc(sizeof(Node));    if(L == NULL)  printf("内存分配失败!\n");    L->next = NULL;    L->prev =NULL;     return L;}void CreateList1(Pnode L,ElemType e)  //头插法 {     Pnode s;     Pnode newNode=(Pnode)malloc(sizeof(Node));     newNode->data=e;     if(L->next == NULL){        L->next=newNode;        newNode->prev =L;        newNode->next =NULL;     } else{        s=L->next;        L->next =newNode;        newNode->prev=L;         newNode->next =s;         s->prev=newNode;      }} void DeleteLink(Pnode L,int i)  //删除操作 {    Pnode s =double_find(L,i);        Pnode a =s->prev;    if(s->next==NULL)    {        a->next =NULL;    }else{       Pnode b =s->next;       a->next=b;       b->prev=a;    }    free(s);     printf("删除操作!\n");    print_list(L);} void  print_list(Node *pHead){    Node *p;    printf("打印链表:\n");    for(p=pHead->next;p!=NULL;p=p->next)    {            printf("%d ",p->data);      }}Pnode double_find(Pnode l,  int num)//查找第I 个结点 传进来头结点 {    Pnode p;    int i=-1;    for(p=l;p!=NULL;p=p->next)    {        i++;        if(i == num)        {            return p;            break;        }    }    if(p==NULL)     printf("没有你要找的位置 ");}
0 0
原创粉丝点击