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

c语言编写 带头结点的单链表操作(头插法)

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct  LNode *next;
    
}LNode;
LNode *create_head(LNode *head);
LNode *create_node(ElemType data);
void Insert_byHead(LNode *head, ElemType data);
int EmptyList(LNode *head);
void Print_List(LNode *head);
int Len_Get(LNode *head);
void Insert_Node(LNode *head, ElemType data);
void Delete_Node(LNode *head, int pos);
void List_Reverse(LNode* head);
int Find_Node(LNode* head, ElemType data);
void Clear_List(LNode* head);

LNode *create_head()//创建头结点
{
    LNode *head = (LNode *)malloc(sizeof(LNode));
    if (!head)return;
    head->data = NULL;
    head->next = NULL;
    return head;
}
LNode *create_node(ElemType data)//创建结点
{
    LNode *newnode = (LNode*)malloc(sizeof(LNode));
    if (!newnode)return;
    newnode->data=data;
    newnode->next = NULL;
    return newnode;
}
void Insert_byHead(LNode *head,ElemType data)//头插法链接
{
    LNode *newnode = create_node(data);
    newnode->next = head->next;
    head->next = newnode;
}
int EmptyList(LNode *head)
{
    if (head->next == NULL)return 1;
    return 0;
}
void Print_List(LNode *head)//遍历打印
{
    if (EmptyList(head))return;
    LNode *p = head->next;
    while (p)
    {
        printf("%d",p->data);
        p = p->next;
    }

}
int Len_Get(LNode *head)//遍历获取长度
{
    int len = 0;
    LNode *p = head->next;
    while (p)
    {
        p = p->next;
        len++;
    }
    return len;
}
void Insert_Node(LNode *head, ElemType data,int pos)//插入第几个结点后
{
    if (EmptyList(head))return;
    if (pos > Len_Get(head))
    {
        printf("error");
        return;
    }
    LNode *newnode = create_node(data);
    LNode *p = head;
    LNode *tmp;
    int i = 0;
    while (i != pos)
    {
        p = p->next;
        i++;
    }
    tmp = p->next;
    p->next = newnode;
    newnode->next = tmp;
}
void Delete_Node(LNode *head,int pos)//删除结点
{
    if (EmptyList(head))return;
    if (pos > Len_Get(head))
    {
        printf("error");
        return;
    }
    LNode *p = head;
    LNode *tmp;
    int i = 1;
    while (i != pos)
    {
        p = p->next;
        i++;
    }
    tmp = p->next;
    p->next = tmp->next;
    free(tmp);
}
void List_Reverse(LNode* head)//链表转置
{
    if (EmptyList(head))return;
    LNode *p1, *p2, *p3;
    p1 = head->next;
    p2 = p1->next;
    p3 = p2->next;
    head->next = NULL;
    p1->next = NULL;
    while (p3->next)
    {
        p2->next = p1;
        p1 = p2;
        p2 = p3;
        p3 = p2->next;    
    }
    p2->next = p1;
    p3->next = p2;
    head->next = p3;
}
void Clear_List(LNode* head)//链表清空
{
    LNode *p,*tmp;
    p = head->next;
    
    while (p)
    {
        tmp = p;
        p = p->next;
        free(tmp);
    }
    head->next = NULL;
}
int Find_Node(LNode* head,ElemType data)//链表根据查找的元素查询位置
{
    if (EmptyList(head))return;
    int pos=1;
    LNode *p = head->next;
    while (p)
    {
        if(p->data==data)
        { 
            return pos;
        }
        p = p->next;
        pos++;
    }
    
}
void main()
{
    LNode *L = create_head();
    Insert_byHead(L, 5);
    Insert_byHead(L, 7);
    Insert_byHead(L, 9);
    Insert_byHead(L, 6);
    Insert_byHead(L, 4);
//    Insert_Node(L, 5, 1);
//    Delete_Node(L, 6);
    List_Reverse(L);
//    Clear_List(L);
    Print_List(L);
    printf("%d",Find_Node(L,4));
    system("pause");
}

 

posted @ 2019-05-05 21:52  忘心笑  阅读(4792)  评论(0编辑  收藏  举报