带头结点的双向链表操作(头插法)-c语言实现

带头结点的双向链表操作(头插法)-c语言实现

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
typedef int ElemType;
typedef struct  LNode
{
    ElemType data;
    struct  LNode *pri;
    struct  LNode *next;
}LNode;


LNode* create_head();
LNode* create_node(ElemType data);
void InsertbyHead(LNode* head,ElemType data);
int Empty_List(LNode* head);
void Forward_Print_List(LNode* head);
void Opposite_Print_List(LNode* head);
void Insert_Node(LNode* head,ElemType data,int pos);
void Delete_Node(LNode* head,int pos);
int Find_Node(LNode* head,ElemType data);

LNode* create_head()
{
    LNode *head=(LNode*)malloc(sizeof(LNode));
    head->data=NULL;
    head->next=head;
    head->pri=head;
    return head;
}
LNode* create_node(ElemType data)
{
    LNode *newnode=(LNode*)malloc(sizeof(LNode));
    newnode->data=data;
    newnode->next=NULL;
    newnode->pri=NULL;
    return newnode;
}
void InsertbyHead(LNode* head,ElemType data)
{
    LNode *newnode=create_node(data);
    newnode->next=head->next;
    head->next=newnode;
    newnode->next->pri=newnode;
    newnode->pri=head;
}
int Empty_List(LNode* head)
{
    if(head->next==head)
    return 1;
    return 0;
}
void Forward_Print_List(LNode* head)
{
    if(Empty_List(head))return;
    LNode *p;
    p=head->next;
    while(p!=head)
    {
        printf("%d",p->data);
        p=p->next;
    }
}
void Opposite_Print_List(LNode* head)
{
    if(Empty_List(head))return;
    LNode *p;
    p=head->pri;
    while(p!=head)
    {
        printf("%d",p->data);
        p=p->pri;
    }
}
int Len_Get(LNode* head)
{
    if(Empty_List(head))return 0;
    LNode *p;
    int len=0;
    p=head->next;
    while(p!=head)
    {
        len++;
        p=p->next;
    }
    return len;
}
void Insert_Node(LNode* head,ElemType data,int pos)
{
    if(pos>Len_Get(head))return;
    LNode *newnode=create_node(data);
    LNode *p=head;
    int i=0;
    while(i!=pos)
    {
        p=p->next;
        i++;
    }
    newnode->next=p->next;
    p->next=newnode;
    newnode->next->pri=newnode;
    newnode->pri=p;    
}
void Delete_Node(LNode* head,int pos)
{
    if(pos>Len_Get(head))return;
    LNode *p=head;
    int i=0;
    while(i!=pos)
    {
        p=p->next;
        i++;
    }
    p->pri->next=p->next;
    p->next->pri=p->pri;
    free(p);
}
int Find_Node(LNode* head,ElemType data)
{
    int pos=1;
    LNode *p=head->next;
    while(p!=head)
    {
        if(p->data==data)return pos;
        pos++;
        p=p->next;
    }
    return 0;
}
void Clear_List(LNode* head)
{
    LNode *p=head->next;
    LNode *tmp;
    head->pri=head;
    head->next=head;
    while(p->next!=head)
    {
        tmp=p;
        p->pri=NULL;
        p=p->next;
        free(tmp);
    }
    p->next=NULL;
    p->pri=NULL;
    free(p);
    
} 
void main()
{
    LNode *L=create_head();
    InsertbyHead(L,4);
    InsertbyHead(L,5);
    InsertbyHead(L,7);
    InsertbyHead(L,9);
    InsertbyHead(L,8);
    Insert_Node(L,7,1);
    Delete_Node(L,1);
    Clear_List(L);
    Forward_Print_List(L);
//    Opposite_Print_List(L);
    printf(" 查找的元素位置为:%d",Find_Node(L,9));
    printf(" 长度为%d",Len_Get(L));
    system("pause");
}

猜你喜欢

转载自www.cnblogs.com/ljh-blog/p/10820677.html