单项链表的一些问题(约瑟夫问题)

#ifndef _LIST_H
#define _LIST_H


typedef struct node
{
int data;//结点的数据域
struct node *next; //结点 的后继指针域


}Node;


Node *creatList(int n);//创建链表,返回值为结构体的首地址
void PrintListLen(Node *h);//形参为Node 的指针
int getList(Node *h);//链表的长度
void insertList(Node *h,int pos,int num);//在pos处插入一个结点
void destroyList(Node *h);//删除链表
void verse(Node *h); //反序排列
Node *Adjmax(Node *h); //相邻两节点data值之和最大的地址

#endif


#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "list.h"




Node *creatList(int n)
{
Node *p,*r,*H;
int i;
H=(Node*)malloc(sizeof(Node));//头结点,无内容

if(NULL == H)
{
                 
}


        r = H; //保留起始地址,以便返回链表的首地址
        
        for(i = 0; i<n; i++)
        {
            p=(Node*)malloc(sizeof(Node));
            if(p == NULL)
            {


            }


            p->data = i+100;
            r->next = p;
            r = p;
        }


         r->next = NULL;


         return H;  
           
       
}




void PrintList(Node *h)
{
        Node *p = h;
        p = p->next;  //第0项为空内容,跳过显示,从第一项开始
        while(p)
        {
            printf("%d\n",p->data);
            p = p->next;
        }
}


int getListLen(Node *h)
{
        int len = 0;
        while(h->next)
        {
            len++;
            h = h->next;  
        }


        return len;
}
void insertList(Node *h,int pos,int num)
{
        Node *r = h,*p;
        int i;
        if(pos>getListLen( h)||pos<0)
        {
            return ;
        }


        for(i = 0; i<pos; i++)
        {
            r = r->next;
        }


        p=(Node *)malloc(sizeof(Node));


        p->next = r->next; //将p后面与r接上
        r->next = p;           //将p的前面与r接上
        p->data = num;
}




void destroyList(Node *h)
{
        Node *p=h, *q;
        while(p->next)     //因为建立链表时第一个内容为空,所以要指向next
        {
            q = p->next; //先头后尾
            p->next = q->next;//与插入的顺序相反
            free(q);
        }
        free(p);
}


/*倒序*/
void verse(Node *h)
{
        Node *p,*q;
        p = h->next;
        h->next = NULL;//原链表置空。第一次循环时。第一个结点的next指向null,即:链表的最后位置
        
        while(p)
        {
            q=p;
            p = p->next ;
            q->next = h->next;
            h->next = q;            
        }
         
}


/*相邻亮点data值之和最大的地址*/
Node *Adjmax(Node *h)
{
    Node *p, *p1, *q;
    int m0,m1;
    p=h->next;
    p1=p;                 //保留地址,留着返回用
    if(p1==NULL) 
    {
        return (p1);  //表空返回
    }
    
    q = p->next;
    if(NULL == q)
    {
        return (p1);  //表长等于1是返回
    }


    m0 = q->data + p->data;   //相邻两结点data值之和
    while(q->next)
    {
        p=q;
        q=q->next; //取下一对相邻节点的指针
        m1 = p->data + q->data;
        if(m1>m0)
        {
            p1=p;
            m0=m1;
        }        
    }


    return (p1);
    
    
}
/*约瑟夫问题*/
void josehpu(Node *L, int n,k,m)
{
int i;
Node *p,*r;
L = NULL; //置空表
/*建立循环链表*/
for(i=0; i<=n; i++)
{
p = (Node *)malloc(sizeof(Node));
p->date = i;
if(L == NULL)
{
L = p;
}
else
{
r->next=p;
}
r = p;
}


/*将链表环起来*/
p->next = L;
p = L;
/*找到第k个节点*/
for(i=1; i<=k-1; i++)
{
r= p;
p=p->next;
}


while(p->next != p) //节点数大于1时
{
for(i=1; i<m;i++)
{
r=p;
p= p->next;
}
printf("%d\n", p->data);
r->next = p->next;
free(p);//删除该节点
p=r->next;//取下一个报数的节点指针
}
printf("%d\n", p->data); //打印最后一个出来的数

}


int main ()
{
    Node *p;
    int len = 0;
    p = creatList(5);
    PrintList(p);


    len = getListLen(p);
    printf("%d\n",len);
    
    insertList(p,3,500);
    PrintList(p);
    
    len = getListLen(p);
    printf("%d\n",len);


     verse(p);
     PrintList(p);


     printf("----------------");
     
     p= Adjmax(p);
     printf("%d\n",p->data);


   // destroyList(p);
    return 0;


    
}


100
101
102
103
104
5
100
101
102
500
103
104
6
104
103
500
102
101
100
----------------103
6
2
7
4
3
5
1
8






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值