单链表增删改查代码实现/约瑟夫环代码实现

来源:互联网 发布:维控触摸屏软件 编辑:程序博客网 时间:2024/05/04 17:30

#include <stdio.h>

#include <stdlib.h>

//定义结点

struct node

{

    int data;

    struct node *next;

};


/***********************实现函数********************************/



//找单链表中值为key的结点

struct node *search(struct node *head,int key)

{

    struct node *p;                               //游标

    p = head->next;

    while (p!=NULL) {

        if (p->data==key) {

            return (p);

        }else

        {

            p=p->next;

        }

    }

    return NULL;

}



//单链表结点的插入,p结点后插入值为key的结点

void insert (struct node *p,int key)

{

    struct node *q;

    q = (struct node *)malloc(sizeof(struct node));

    if (!q) {

        printf("不能分配内存空间!");

        exit(0);

    }

    q->data = key;

    q->next = NULL;

    q->next = p->next;

    p->next = q;

}



//单链表结点的删除

int del(struct node *head,int key)

{

    struct node *p,*q;

    int flag = 0;

    p = head;

    while (p->next!=NULL) {

        if (p->next->data==key) {

            flag = 1;

            break;

        }else

            p = p->next;

    }//如果结点中存在值为key的结点,找到其前驱结点pflag变量置1;

    if (flag==1) {

        q = p->next;

        p->next = q->next;

        free(q);

        return 1;

    }else

        return 0;

}



//逆序建链表

struct node *creat1(int n)

{

    struct node * head, * p;

    int i;

    head = (struct node *)malloc(sizeof(struct node));

    head->next = NULL;

    for (i=0; i<n; i++) {

        p = (struct node *)malloc(sizeof(struct node));

        scanf("%d",&p->data);

        p->next = head->next;

        head->next = p;

    }

    return (head);

}



//打印链表

void display(struct node *head, int n)

{

    int i;

    head=head->next;

    for (i=0; i<n; i++) {

        printf("%d ",head->data);

        head=head->next;

    }

    printf("\n");

}


//顺序建链表

struct node *creat2(int n)

{

    struct node  * head,* tail,* p;

    int i;

    head = (struct node *)malloc(sizeof(struct node));

    head->next = NULL;

    tail = head;

    for (i=0; i<n; i++) {

        p = (struct node *)malloc(sizeof(struct node));

        scanf("%d",&p->data);

        p->next = NULL;

        tail->next = p;

        tail = p;

    }

    return (head);

}



//单链表的逆置(带头结点)

void reverse(struct node * head)

{

    struct node * p,* q;

    p = head->next;

    head->next = NULL;

    q = p->next;

    while (p!=NULL) {

        p->next = head->next;

        head->next = p;

        p = q;

        if (q!=NULL) {

            q = q->next;

        }

    }

}



//两个带头结点的有序单链表的归并

struct node * merge(struct node *head1,struct node *head2)

{

    struct node *p1,*p2,*tail;

    p1 = head1->next;

    p2 = head2->next;

    tail = head1;

    free(head2);

    while (p1&&p2) {

        if (p1->data < p2->data) {

            tail->next = p1;

            tail = p1;

            p1 = p1->next;

        }else{

            tail->next = p2;

            tail = p2;

            p2 = p2->next;

        }

    }

    if (p1)

        tail->next = p1;

    else

        tail->next = p2;

    return (head1);

}



//单链表的拆分(拆分为负整数和非负整数)

struct node * split(struct node * head1)

{

    struct node *head2,*p,*q;

    head2 = (struct node *)malloc(sizeof(struct node));

    head2->next = NULL;

    p = head1->next;

    head1->next = NULL;

    q = p->next;

    while (p!=NULL) {

        if (p->data >= 0) {

            p->next = head1->next;

            head1->next = p;

        }else

        {

            p->next = head2->next;

            head2->next = p;

        }

        p = q;

        if (q!=NULL) {

            q = q->next;

        }

    }

    return (head2);

}



//循环链表创建,赋初始值

struct node *creat(int n)

{

    int i;

    struct node *p,*tail,*head;

    p = (struct node *)malloc(sizeof(struct node));

    p->data = 1;

    p->next = NULL;

    head = p;

    tail = p;

    for (i=2; i<=n; i++) {

        p = (struct node *)malloc(sizeof(struct node));

        p->data = i;

        tail->next = p;

        tail = p;

        p->next = NULL;

    }

    tail->next = head;//最后一个结点的指针域指向第一个结点

    return head;

}



//循环建链表、自定义数据

struct node *creat3(int n)

{

    int i;

    struct node *p,*tail,*head;

    p = (struct node *)malloc(sizeof(struct node));

    scanf("%d",&p->data);

    p->next = NULL;

    head = p;

    tail = p;

    for (i=0; i<n-1; i++) {

        p = (struct node *)malloc(sizeof(struct node));

        scanf("%d",&p->data);

        tail->next = p;

        tail = p;

        p->next = NULL;

    }

    tail->next = head;//最后一个结点的指针域指向第一个结点

    return head;

}



//约瑟夫环

int sel(struct node *head,int m,int n)//n个数m删除位置

{

    int num = 0;

    int count = 0;

    struct node *p,*q;

    q = head;

    while (q->next != head) {

        q = q->next;

    }

    printf("被删掉的序号为:");

    while (count < n-1) {

        p = q->next;

        num++;

        if (num%m==0) {

            q->next = p->next;

            printf("%3d",p->data);

            free(p);

            count++;

        }else

            q = p;

    }

    return q->data;//最后一个结点的数据域为所求的结果

}



/**************************主函数********************************/


int main(int argc, const char * argv[])

{

    int n,m;

    struct node *head;

//    scanf("%d %d",&n,&m);

//    head = creat3(n);

//    printf("\n%d\n",sel(head, m, n));

    scanf("%d",&n);

    head = creat2(n);

    display(head, n);

    return 0;

}



0 0