线性表

  这学期学数据结构,打算从现在开始就记笔记,不要向上学期学C语言一样,眼高手低,感觉总结以下比较好,虽然很基础,坚持下去。

顺序存储

  1. 优点:
    1. 用数组存储数据元素,物理位置上的相邻表示数据元素之间逻辑相邻
    2. 无须为表示节点间的逻辑关系而增加额外的存储开销
    3. 存储密度高
    4. 能随机地存取数据元素。
  2. 缺点:
    1. 在进行插入、删除时需要移动大量数据元素,运行效率低
    2. 而且需要预先分配存储空间,如果估计过大,容易造成存储空间浪费;如果估计过小,容易造成数据溢出。

链表的优缺点刚好与顺序表相反。

链式存储

常见操作

/*************************************************************************
    > File Name: 链表.c
    > Author: Tanswer_
    > Mail: 98duxm@gmail.com
    > Created Time: 2016年09月05日 星期一 21时01分57秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct Node
{
    int data;
    struct Node *next;
};

/*单链表创建*/
struct Node *Create()
{
    int x;
    struct Node *head,*r;
    head = (struct Node *)malloc(sizeof(struct Node));
    head -> next = NULL;

    scanf("%d",&x);
    while(x != -1)
    {
        r = (struct Node *)malloc(sizeof(struct Node ));

        r -> data = x;
        r -> next = head -> next;
        head -> next = r;

        scanf("%d",&x);
    }
    return head;
}

/*单链表逆置*/
struct Node *Reverse(struct Node *head)
{
    struct Node *p,*q;
    p = head -> next;
    head -> next = NULL;  //置为空

    while(p)
    {
        q = p;
        p = p -> next;
        q -> next = head -> next;
        head -> next = q;
    }
    return head;
}

/*删除重复节点*/
struct Node * pur_list(struct Node *head)
{
    struct Node *p,*q,*r;
    p = head -> next;
    if(p != NULL)
    while(p -> next != NULL)
    {
        q = p;
        while(q -> next != NULL)
        {
            if(q -> next -> data ==  p ->data)
            {
                r = q -> next;
                q -> next = r -> next;
                free(r);
            }
            else
                q = q -> next;
        }
        p = p -> next;
    }
    return head;
}

/*链表遍历*/
void Disply(struct Node *head)
{
    struct Node *r;
    r = head -> next;
    while(r != NULL)
    {
        printf("%d\n",r -> data);
        r = r -> next;
    }
}

int main()
{
    struct Node *h;
    h = Create();
    Disply(h);
    //h = Reverse(h);
    h = pur_list(h);
    Disply(h);
    return 0;
}

/***********************************************************************************
    > 问题描述
    > 编号为1,2,3...n的n个人按顺时针方向围坐在一张圆桌上,每人持有一个密码.
    > 开始任选一个正整数作为报数上限m,从第一个人开始按顺时针方向自1开始报数,
    > 报m的那个人出队,将他的密码作为新的m值,从他顺时针方向的下一个人开始重新从1报数,
    > 数到m的那个人出对,如此循环下去,直到所有人出队为止.
 **********************************************************************************/

/*************************************************************************
    > File Name: 约瑟夫环.c
    > Author: Tanswer_
    > Mail: 98duxm@gmail.com
    > Created Time: 2016年09月05日 星期一 22时04分21秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct Node 
{
    int     id;           //编号
    int     password;     //密码
    struct Node *next;
};

/*创建带有n个节点的循环链表*/
struct Node *Create(int n)
{
    int     i;
    int     ipassword;
    struct Node *head,*r,*s;

    head = (struct Node *)malloc(sizeof(struct Node));
    head -> next = NULL;
    s = head;
    for(i=1; i <= n; i++)
    {
        r = (struct Node *)malloc(sizeof(struct Node ));

        printf("请输入第%d个人的密码: ",i);
        scanf("%d",&ipassword);

        r -> id = i;
        r -> password = ipassword;

        r -> next = s -> next;
        s -> next = r;
        s = r;
    }
    s -> next = head -> next;
    return head;
}

/*打印循环链表*/
void printlist(struct Node *head)
{
    struct Node *pCur = head -> next;
    printf("--ID-- --PASSWORD--\n");
    do{
        printf("%3d %7d\n",pCur -> id,pCur -> password);
        pCur = pCur -> next;
    }while(pCur != head -> next);
}

/*运行约瑟夫环问题*/
void JosephusOperate(struct Node *head,int password)
{
    int     flag = 1;
    int     icounter = 0;
    struct Node *pDel,*pCur,*pPre;
    pPre = pCur = head -> next;

    while(pPre -> next != head -> next)
        pPre = pPre -> next;

    while(flag)
    {
        for(icounter=1; icounter<password; icounter++)
        {
            pPre = pCur;
            pCur = pCur -> next;
        }
        if(pPre == pCur) flag = 0;

        pDel = pCur;
        pPre -> next = pCur -> next;
        pCur = pCur -> next;
        password = pDel -> password;
        printf("第%d个人出列(密码:%d)\n",pDel -> id,pDel -> password);
        free(pDel);
    }
    head = NULL;
    getchar();
    getchar();
}

int main()
{
    int     n,m;
    struct Node *head;
    printf("请输入人数n: ");
    scanf("%d",&n);
    printf("请输入初始密码: ");
    scanf("%d",&m);
    head = Create(n);
    printf("\n-----打印循环链表-----\n");
    printlist(head);
    printf("\n-----打印出列情况-----\n");
    JosephusOperate(head,m);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值