数据结构--线性表(单链表C语言实现)

//单链表的练习

#include "stdlib.h"
#include "stdio.h"




void printLinkList(struct node *h);

//单链表数据结构
typedef struct node {
    int data;       //数据域
    struct node *next;   //指针域
}Lnode,*LinkList;



//创建链表 带头结点
LinkList createLinkList(int n)
{
    struct node *h, *p, *q;
    int i;
    p=(struct node *)malloc(sizeof(struct node));
    h=p;
    q=p;
    for(i=0;istruct node *)malloc(sizeof(struct node));

        printf("请输入第%d个元素的值:", i+1);
        scanf("%d", &(p->data));

        q->next=p;
        q=p;
    }
    q->next=NULL;
    return h;
}

//查找链表中某个数据
//查找操作不涉及修改链表中的节点位置,所以依然返回之前的头结点,不作修改
LinkList FindElem(LinkList h){
    LinkList p = h->next;
    int data;
    printf("请输入要查找的元素:\n");
    scanf("%d",&data);
    int i = 1;
    int flag = 0;
    while(p!= NULL){
        if (data == p->data){
            printf("查找到的元素为%d\n",data);
            flag = 1;
            break;
        }
        p = p->next;
        i++;
    }
    if (flag == 1){
        printf("查找到的元素在第%d个位置\n",i);
    } else{
        printf("没找到相关元素\n");
    }
    return h;

}





//向链表中插入数据
LinkList InsertList(LinkList h){
    LinkList p = h->next;
    LinkList q;
    q = (LinkList)malloc(sizeof(Lnode));//为插入的数据申请一个空间
    int data,n;
    printf("请输入插入的数据\n");
    scanf("%d",&data);
    printf("请输入要插入的位置\n");
    scanf("%d",&n);
    int j = 1;
    if (n == 1){
        q->next = p->next;
        q->data = data;
        p->next = q;
        return p;
    }
    while(p!= NULL &&  n > j+1){

        p = p->next;
        j++;
    }
    printf("%d\n",p->data);
    if (p ==NULL){
        printf("参数错误");
        return NULL;
    }
    q->next = p->next;
    q->data = data;
    p->next = q;
    return h;
}


LinkList Insert_Linklist(LinkList h)
//在有序单链表中插入元素,链表仍然有序
{
    LinkList p, q, s;
    int x;
    printf("请输入要插入的元素:\n");
    scanf("%d",&x);
    q = h;
    p = h->next;
    while(p)
    {
        if(p->data <= x)
        {
            q = p;
            p = p->next;
        }
        else
            break;
    }
    s = (LinkList)malloc(sizeof(Lnode));
    if(!s) return 0;
    s->data = x;
    q->next = s;
    s->next = p;
    return h;
}







//插入数据操作2
LinkList InsertList2(LinkList h){
    LinkList p = h->next;
    LinkList q;

    printf("请输入要插入的元素:\n");
    int data,n;
    scanf("%d",&data);
    printf("请输入要插入的元素的位置:\n");
    scanf("%d",&n);
    if(n == 1){//新节点插入到表头
        q = (LinkList)malloc(sizeof(Lnode));//为将要插入的元素申请一个空间
        q->data = data;
        q->next = p;
        h->next = q;
        return h;//返回新表头指针

    } else{

        //查 找第n-1个节点
        int i = 1;
        int flag = 0;
        while(p!= NULL){
            if (n == i+1){
                flag = 1;
                break;
            }
            p = p->next;
            i++;
        }
        printf("%d\n",p->data);
        if (p == NULL){
            printf("参数错误\n");
            return NULL;
        } else {
            q = (struct node *)malloc(sizeof(struct node));
            q->data = data;
            q->next = p->next;
            p->next = q;
            return h;

        }
        return h;
    }

    return h;

}
//删除链表中值为X的数据
LinkList DeleateList(LinkList h){
    LinkList p = h->next;
    LinkList q = h;
    printf("请输入要删除的元素值\n");
    int data;
    scanf("%d",&data);
    while(p != NULL){

        if(p->data == data){
            q->next = p->next;
            free(p);//将p节点释放掉
            p = q->next;//p再指向删除元素所指向的元素
        }else{
            q = p;
            p = p->next;
        }
    }
    return h;
}


//删除链表中所有值为偶数的节点
LinkList DelEven_LinkList(LinkList h){
    LinkList p, q;
    q = h;
    p = h->next;
    while(p)
    {
        if(p->data % 2 == 0)
        {
            q->next = p->next;
            free(p);
            p = q->next;
        }
        else
        {
            q = p;
            p = p->next;
        }
    }
    return h;
}

//链表清空
LinkList ClearList(LinkList h){
    LinkList p = h->next;
    LinkList q;
    while(p != NULL){
        q = p;
        p = p->next;
        free(p);
    }
    h->next = NULL;
    return h;
}


//链表逆转
LinkList Reversed(LinkList h){
    LinkList q=h->next;
    LinkList p=NULL;
    LinkList pNext;
    while (NULL != q) {
        pNext=q->next;
        q->next = p;
        p=q;
        q=pNext;
    }

    h->next = p;
    return h;//返回头指针
}
//计算单链表的长度,并遍历单链表输出单链表表的内容
void printLinkList(struct node *h)
{
    LinkList p=h->next;
    int i = 0;
    while(p!=NULL)
    {
        printf("%d ", p->data);
        p=p->next;
        i++;
    }

    printf("\n");
    printf("单链表的长度为:%d\n",i);
}
void menu(){
    printf("1:链表创建\n");
    printf("2:插入操作\n");
    printf("3:删除操作\n");
    printf("4:查找操作\n");
    printf("5:链表逆转\n");
    printf("6:链表清空\n");
    printf("7:删除值为偶数的节点\n");
    printf("0:退出\n");

}

int main()
{
    LinkList head;

    int m;
    while(1){
        menu();
        printf("请输入操作的序号:\n");
        scanf("%d",&m);
        switch (m) {
            case 1:
                int n;
                //struct node *head;

                printf("请输入元素的个数:");
                scanf("%d", &n);
                head=createLinkList(n);//得到头结点
                printf("\n单链表创建完毕。。。\n");
                printf("\n单链表的元素如下:\n");
                printLinkList(head);

                break;
            case 2:
                //向链表中插入数据
                //head = InsertList(head);
                head = Insert_Linklist(head);//有序插入
                printf("插入数据的链表后的结果为:");
                printLinkList(head);
                break;
            case 3:
                //链表删除操作
                head = DeleateList(head);
                printLinkList(head);
                break;
            case 4:
                //查找链表中某个元素并输出其所在位置
                FindElem(head);

                break;
            case 5:
                //链表反转
                Reversed(head);
                printf("反转过后的链表如下:\n");
                printLinkList(head);

                break;
            case 6:
                //清空链表
                ClearList(head);
                printf("链表清空完成:\n");
                printLinkList(head);

                break;
            case 7:
                DelEven_LinkList(head);
                printLinkList(head);
                break;
            case 0:
                return 0;
                break;

            default:
                break;
        }
    }

}

你可能感兴趣的:(数据结构)