C语言单链表,能直接运行的代码!

C语言单链表,实现增删改查

不废话 直接上代码,COPY就能运行

#include 
#include 
/**
*定义数据元素
*/
typedef struct{
    int id;
    char * name;
    char * gender;
    int age;
}ElemType;


typedef struct node{
    ElemType data; //数据域
    struct Node * next; //指针域
}Node;
/**
*   头结点
*/
typedef struct Head{
    struct Node * next;
    int length; //链表长度
}Head;
/** 初始化链表*/
void InitLink(Head * head,ElemType * array,int length){
    int i;
    for(i = 0;idata = data;
    node->next = head->next;
    head->next = node;
    head->length++;
}
/**尾插法插入元素*/
void InsertLink(Head * head,int pos, ElemType data){
    Node * node = (Node * )malloc(sizeof(Node));
    node->data = data;
    node->next = NULL;
    if(pos==1){
        head->next = node;
        head->length++;
        return;
    }
    Node * p = head->next;
    int i=1;
    while(p!=NULL&&inext;
        i++;
    }
    if(p){
        node->next = p->next;
        p->next = node;
        head->length++;
    }else{
        printf("插入位置不存在!\n");
    }
}
void PrintData(Head * head){
    Node * p = head->next;
    if(!p){
        printf("链表为空!\n");
        head->length = 0;
        return;
    }
    int i;
    for(i=0;ilength;i++){
        printf("%d\t%s\t%d\t%s\n",p->data.id,p->data.name,p->data.age,p->data.gender);
        p = p->next;
    }
}
Node * GetElementByPosition(Head * head,int pos){
    Node * p = head->next;
    int i=1;
    while(p!=NULL&&i!=pos){
        p=p->next;
        i++;
    }
    return p;
}
Node * GetElementById(Head * head,int id){
    Node * p = head->next;
    while(p!=NULL&&p->data.id!=id){
        p=p->next;
    }
    return p;
}
/**删除指定位置的元素返回该元素地址*/
ElemType  DeleteElemByPos(Head * head,int pos){
    if(pos<1&&pos>head->length){
        printf("删除位置不存在!");
        return;
    }
    ElemType elem;
    Node * p = NULL;
    if(pos==1){
        elem = p->data;
        p = head->next;
        head->next = p->next;
        free(p);
        head->length--;
    }
    Node * preNode; //前驱结点
    p = head->next;
    int i=1;
    while(p!=NULL&&inext;
        i++;
    }
    if(p){
        elem = p->data;
        preNode->next = p->next;//将后继结点的指针域赋值给前驱结点的指针域
        free(p);//释放内存空间
        head->length--;
    }
    return elem;
}
void menu(){
    int n;
      ElemType arrays[4] = {
    {1001,"张三","男",21},
    {1003,"刘萌","女",19},
    {1004,"李强","男",20},
    {1002,"李瑶","女",18}
    };
    Head head;
    head.length = 0;
    InitLink(&head,arrays,sizeof(arrays)/sizeof(arrays[0]));
    while(n!=6){
        printf("1,遍历输出数据\n");
        printf("2,按位置查找\n");
        printf("3,按学号查找\n");
        printf("4,在位置i处插入元素\n");
        printf("5,删除位置i处的元素\n");
        printf("6,退出\n");
        printf("请选择序号1-5\n");
        scanf("%d",&n);
        switch(n){
        case 1:
            PrintData(&head);
            break;
        case 2:
            {
                 printf("请输入要查找的位置:\n");
                 int n;
                 scanf("%d",&n);
                 Node * node = GetElementByPosition(&head,n);
                 printf("查询到的数据:\n");
                 printf("%d\t%s\t%s\t%d\n",node->data.id,node->data.name,node->data.gender,node->data.age);
            }
            break;
        case 3:
              {
                 printf("请输入要查找的学号:\n");
                 int n;
                 scanf("%d",&n);
                 Node * node = GetElementById(&head,n);
                 printf("查询到的数据:\n");
                 printf("%d\t%s\t%s\t%d\n",node->data.id,node->data.name,node->data.gender,node->data.age);
            }
            break;
        case 4:
            {
                 ElemType newNode;
                 printf("请输入要插入的学生id:\n");
                 scanf("%d",&newNode.id);
                 printf("请输入要插入的学生姓名:\n");
                 newNode.name = (char *)malloc(10);
                 scanf("%s",newNode.name);
                 printf("请输入要插入的学生性别:\n");
                 newNode.gender = (char *)malloc(10);
                 scanf("%s",newNode.gender);
                 printf("请输入要插入的学生年龄:\n");
                 scanf("%d",&newNode.age);
                 printf("请输入要插入的位置\n");
                 int pos;
                 scanf("%d",&pos);
                 InsertLink(&head,pos,newNode);
            }
            break;
        case 5:
            {
                printf("请输入你要删除元素的位置:\n");
                int n;
                scanf("%d",&n);
                ElemType data = DeleteElemByPos(&head,n);
                printf("已经删除的元素为:\n");
                printf("%d\t%s\t%s\t%d\n",data.id,data.name,data.gender,data.age);
            }
            break;
        case 6:
            printf("退出成功!\n");
            exit(1);
            break;
        default:
            printf("输入错误请重新输入1-6!!!");
            break;
        }
    }
}
int main()
{
    menu();
    return 0;
}

如果对你有帮助点个赞吧!

路漫漫其修远兮

你可能感兴趣的:(C语言)