链表

笔记:
对头部操作:


/*************************************************************************
    > File Name: head.c
    > Author: pengrenchang123
    > Mail: [email protected] 
    > Created Time: Wed 21 Dec 2016 09:52:16 AM CST
 ************************************************************************/

#include
#include      //malloc free
#include
#include     //sleep

typedef struct student{
    int ID;
    char name[32];
    struct student *next;
}STU,*pSTU;
//创建节点
pSTU create_node(){
    pSTU newNode = (pSTU)malloc(sizeof(STU));
    
    if(newNode == NULL)
        newNode = (pSTU)malloc(sizeof(STU));

    newNode->next = NULL;
    printf("创建新节点成功!!!\n");
    return newNode;
}
//随意添加
pSTU add_random_node(pSTU head,int position){
    pSTU p;
    pSTU temp = create_node();
    printf("input ID:");
    scanf("%d",&temp->ID);
    printf("input name:");
    scanf("%s",temp->name);

    int i = 0;
    for(p = head;p->next != NULL;p = p->next,i++){
        if(i == position-1 && p->next != NULL){
            temp->next = p->next;
            p->next = temp;
            temp = NULL;
            return head;
        }
    }

        if(i == position-1 && p->next == NULL){
            p->next = temp;
            temp = NULL;
            return head;
        }
}
//随意删除
pSTU delete_random_node(pSTU head,int position){
    pSTU p1 = head;
    pSTU p2 = head->next;

    int i = 1;
    for(;p2 != NULL;p2 = p2->next,p1 = p1->next,i++){
        if(position == i){
            p1->next = p2->next;
            free(p2);
            p2 = NULL;
            return head;
        }
    }
    if(p2 == NULL && position == i){
        p1->next = NULL;
        free(p2);
        p2 = NULL;
        return head;
    }
}
//从头部添加
pSTU add_head_node(pSTU head){

    pSTU temp = create_node();
    printf("input ID:");
    scanf("%d",&temp->ID);
    printf("input name:");
    scanf("%s",temp->name);

    temp->next = head->next;
    head->next = temp;
    temp = NULL;
    return head;
}
//显示链表
void show_link(pSTU head){
    pSTU p = head->next;
    printf("\tID\tname\n");
    
    while(p != NULL){
        printf("\t%d\t%s\n",p->ID,p->name);
        p = p->next;
    }
}
//头删
pSTU delete_head_node(pSTU head){

    pSTU temp = head->next;

    if(temp == NULL){
        printf("链表无数据可删除!!\n");
        return head;
    }
    head->next = temp->next;
    free(temp);
    temp = NULL;

    return head;
}
//修改
pSTU modify_node(pSTU head){
    char name[32] = {0};
    printf("请输入要修改的姓名:");
    scanf("%s",name);
    pSTU p;
    for(p = head->next;p != NULL;p = p->next){
        if(strcmp(name,p->name) == 0){
            printf("\t该用户的信息为:\n");
            printf("\tname:%s\n\tID:%d\n",p->name,p->ID);
            printf("请输入用户的新ID:");
            scanf("%d",&p->ID);
            return head;
        }
    }
    if(p == NULL)
        printf("\t没有该节点\n");
    return head;
}
//查找节点,按姓名查找
int find_node(pSTU head){
    char name[32] = {0};
    printf("请输入要查找的姓名:");
    scanf("%s",name);

    pSTU p;
    int count = 0;
    for(p = head->next;p != NULL;p = p->next){
        count++;
        if(strcmp(name,p->name) == 0){
            printf("查到了节点\t为第%d个\n",count);
            return count;
        }
    }
    if(NULL == p){
        printf("为找到该节点!!!\n");
        return 0;
    }
}
//显示菜单
void menu(pSTU head){
    int choice = 0;

    while(1){
        system("clear");
        printf("\t\t1.创建链表\n");
        printf("\t\t2.添加节点\n");
        printf("\t\t3.删除链表\n");
        printf("\t\t4.查看链表\n");
        printf("\t\t5.修改链表\n");
        printf("\t\t6.查找链表\n");
        printf("\t\t7.随意添加链表\n");
        printf("\t\t8.随意删除链表\n");
        printf("\t\t0.退出\n");
        printf("\t请选择:");
        scanf("%d",&choice);

        switch(choice){
            case 1:
                head = create_node();
                break;
            case 2:{
                int i = 0;
                for(i = 0;i<4;i++)
                    head = add_head_node(head);
        
                break;
           }
            case 3:
                head = delete_head_node(head);
                break;
            case 4:
                show_link(head);
                sleep(3);
                break;
            case 5:
                head = modify_node(head);
                sleep(2);
                break;
            case 6:
                find_node(head);
                sleep(2);
                break;
            case 7:{
                    int position = 0;
                    printf("输入插入的位置");
                    scanf("%d",&position);
                    add_random_node(head,position);
                    break;
             }
            case 8:{
                    int position = 0;
                    printf("请输入要删除的位置");
                    scanf("%d",&position);
                    delete_random_node(head,position);
                    break;
            }
            case 0:
                exit(1);
                break;
            default:
                printf("\t请重新输入!!\n");
                break;
        }

    }
    return;
}

//程序入口
int main(int argc,char *argv[]){
    pSTU head = create_node();
    menu(head);
    return 0;
}

对尾部操作

/*************************************************************************
    > File Name: linkTail.c
    > Author: pengrenchang123
    > Mail: [email protected] 
    > Created Time: Wed 21 Dec 2016 03:05:08 PM CST
 ************************************************************************/

#include
#include
#include
#include

typedef struct student{
    int ID;
    char name[32];
    struct student *next;
}STU,*pSTU;

pSTU create_link(pSTU head){
    if(NULL == head){
        head = (pSTU)malloc(sizeof(STU));
        head->next = NULL;
    }
    printf("\t创建链表成功\n");
    return head;
}

pSTU create_new_node(){
    pSTU temp = (pSTU)malloc(sizeof(STU));
    if(temp == NULL){
        temp = (pSTU)malloc(sizeof(STU));
    }
    temp->next = NULL;
    printf("input ID:");
    scanf("%d",&temp->ID);
    printf("input name:");
    scanf("%s",temp->name);
    return temp;
}

pSTU add_tail_node(pSTU head){
    pSTU temp = create_new_node();
    pSTU p = head;
    while(p->next != NULL){
        p = p->next;
    }
    p->next = temp;
    temp = NULL;
    return head;
}

pSTU delete_tail_node(pSTU head){
    pSTU p = head;
    if(p->next == NULL){
        printf("\tlink is empty!!!\n");
        return head;
    }
    for(;p->next->next != NULL;p = p->next);
    free(p->next);
    p->next = NULL;
    return head;
}

void show_link(pSTU head){
    pSTU p = head->next;
    printf("\t\tID\tname\n");
    while(p != NULL){
        printf("\t\t%d\t%s\n",p->ID,p->name);
        p = p->next;
    }
}

void menu(){
    pSTU head;
    int choice = 0;

    system("clear");
    while(1){

        printf("\t\t1.创建链表\n");
        printf("\t\t2.添加链表\n");
        printf("\t\t3.尾部添加链表\n");
        printf("\t\t4.尾部删除链表\n");
        printf("\t\t5.显示链表\n");
        printf("\t\t0.退出\n");
        printf("\t请选择:");
        scanf("%d",&choice);
        
        switch(choice){
            case 1:
                head = create_link(head);
                break;
            case 2:{
                int i = 0;
                for(i = 0;i<4;i++)
                    head = add_tail_node(head);
                break;
            }
            case 3:
                head = add_tail_node(head);
                break;
            case 4:
                head = delete_tail_node(head);
                break;
            case 5:
                show_link(head);
            //  sleep(1);
                break;
            case 0:
                exit(1);
                break;
            default:
                printf("请重新输入!!");
                break;
        }
    }
}

int main(int argc,char *argv[]){
    menu();
    return 0;
}

双头链表

/*************************************************************************
    > File Name: doubleHead.c
    > Author: pengrenchang123
    > Mail: [email protected] 
    > Created Time: Wed 21 Dec 2016 07:08:38 PM CST
 ************************************************************************/

#include
#include
#include
#include
typedef struct student{
    int ID;
    char name[32];
    struct student *pre;
    struct student *next;
}STU,*pSTU;

pSTU create_node(){
    pSTU node = (pSTU)malloc(sizeof(STU));
    if(node == NULL)
        node = (pSTU)malloc(sizeof(STU));
    node->next == NULL;
    node->pre == NULL;
    printf("input ID name:");
    scanf("%d %s",&node->ID,node->name);
    return node;
}

//头插
pSTU add_head_doulist(pSTU head){
    pSTU temp = create_node();
    if(head == NULL){
        head->next = temp;
        temp->pre = head;
        return head;
    }
    else{
        temp->next = head->next;
        temp->pre = head;
        head->next = temp;
        head->next->pre = temp;

        return head;
    }
}

//头删
pSTU delete_head_node(pSTU head){
    pSTU p = head->next;
    if(p == NULL){
        printf("list is empty!\n");
        return head;
    }else{
        head->next = p->next;
        p->next->pre = head;
        free(p);
        p->next = NULL;
        p->pre = NULL;
        return head;
    }
}

//尾插
pSTU add_tail_dounode(pSTU head){
    pSTU p = head;
    pSTU temp = create_node();
    while(p->next != NULL)
        p = p->next;
    p->next = temp;
    temp->pre = p;
    temp = NULL;
    return head;
}
//尾删
pSTU delete_tail_dounode(pSTU head)
{
    pSTU p = head;
    if(head->next == NULL)
    {
        printf("link is empty!!!\n");
        return head;
    }
    for(;p->next != NULL;p = p->next);
    p = p->pre;
    free(p->next);
    p->next = NULL;
//p->next->pre == NULL;
    return head;
}

void show_doulist(pSTU head){
    pSTU p = head->next;
    printf("\tID\tname\n");
    while(p != NULL){
        printf("\t%d\t%s\n",p->ID,p->name);
        p = p->next;
    }
}

int main(){
    pSTU head;
    head = (pSTU)malloc(sizeof(STU));
    head->pre = NULL;
    head->next = NULL;
    int i = 0;
    for(;i<3;i++)
//      head = add_head_doulist(head);
//  show_doulist(head);
//  head = delete_head_node(head);
//  show_doulist(head);
        head = add_tail_dounode(head);
    show_doulist(head);
    head = delete_tail_dounode(head);
    head = delete_tail_dounode(head);
    show_doulist(head);
    return 0;
}

你可能感兴趣的:(链表)