005 -- DuLinkList_add nodes, delete node, Caser print..

本文介绍了一个使用双向链表实现字母循环的功能。通过初始化双向链表并填充26个英文字母,用户可以输入一个整数来决定字母序列的偏移量。该程序能够根据用户输入正向或负向移动字母顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

000-- define a Dulinklist

A empty DuLinklist:

 

typedef struct DualNode{
    ElemType data;
    struct DualNode *prior;
    struct DualNode *next;
    
}DualNode, *DuLinkList;

 

 

  •  Add a new Node

 

 

 

s->next = p;    
s->prior = p->prior;    
p->prior->next = s;    
p->prior = s;

 

 

  • Delete a Node

 

 

 

p->prior->next = p->next;
p->next->prior = p->prior;    
free(p);

 

 print A-Z with the defined sequence by user

 

e.g.  user input 3

then should be DEFG....ZABC

user input -3

then should be XYZABCEFG.....W

 

#define OK 1;
#define ERROR 0;

typedef char ElemType;
typedef int Status;
typedef struct DualNode;
{
    ElemType data;
    struct DualNode *prior;
    struct DualNode *next;
    
}DualNode, *DuLinkList;

Status InitList(DuLinkList *L)
{
    DualNode *p,*q;
    int i;
    
    *L = (DuLinkList)malloc(sizeof(DualNode));
    
    if(!(*L))
        return ERROR;
    
    (*L)->next = (*L)->prior = NULL;
    
    p = (*L);
    
    for(i=0;i<26;i++)
    {
        q = (DualNode *)malloc(sizeof(DualNode));
        
        if(!q)
            return ERROR;
        
        q->data = 'A'+i;
        q->prior = p;
        q->next = p->next; 
        p->next = q;
        p=q;
    }
    
    p->next = (*L)->next; //the last Node points to the first Node
    (*L)->next->prior = p; //set the first Node with data 'A' as head Node
    
    return OK;
}


void caser(DuLinkList *L, int i)
{
    if(i>0)
    {
        do{
            (*L) = (*L)->next;
        } while(--i);
    }
    
    if(i>0)
    {
        i = i-1;
        (*L)=(*L)->next;
        
        do{
            (*L) = (*L)->prior;
            
        }while (++i);
        
    }
    
}

int main()
{
    DuLinkList L;
    int i n;
    InitList(&L);
    printf("Please input a integer: \n");
    scanf("%d", &n);
    printf("\n");
    
    caser(&L,n);
    
    for(i=0;i<26;i++)
    {
        L = L->next;
        printf("%c", L->data);
        
    }
    
    printf("\n");
    
    return 0;
}

 

转载于:https://www.cnblogs.com/Shareishappy/p/7545169.html

#include <stdio.h> #include <stdlib.h> const int Max_length=55;//最大内存 struct areaNode//管理分区的结构体 { int ID;//分区号 int size;//分区大小 int address;//分区地址 int flag;//使用状态,0为未占用,1为已占用 }; typedef struct DuNode//双向链表结点 { struct areaNode data;//数据域 struct DuNode *prior;//指针域 struct DuNode *next; }*DuLinkList; DuLinkList m_head = new DuNode, m_last = new DuNode;//双向链表首尾指针 void init()//分区链表初始化 { m_head->prior = NULL; m_head->next = m_last; m_last->prior = m_head; m_last->next = NULL; m_head->data.size = 0; m_last->data.address = 0; m_last->data.size = Max_length; m_last->data.ID = 0; m_last->data.flag = 0; } void show() { DuNode *p = m_head->next;//指向空闲区队列的首地址 printf("+++++++++++++++++++++++++++++++++++++++\n"); while (p) { printf("分区号:"); if (p->data.ID == 0) printf("FREE\n"); else printf("%d\n",p->data.ID); printf("分区起始地址:%d\n",p->data.address); printf("分区大小:%d\n",p->data.size); printf("分区状态:"); if (p->data.flag) printf("已被占用\n"); else printf("空闲\n"); printf("——————————————————\n"); p = p->next; } } bool first_fit(int id,int m_size)//首次适应算法,id为作业号,m_size为作业大小 { //请补充使用首次适应算法给作业分配内存的函数代码 } void recycle(int id)//回收内存,id为释放内存的作业号 { //请补充回收作业内存的代码 } int main() { init(); printf("首次适应算法:\n"); first_fit(1,15); first_fit(2,30); recycle(1); first_fit(3,8); first_fit(4,6); recycle(2); show(); DuNode *p = m_head; while(p != NULL) { DuNode *temp = p; p = p->next; delete(temp); temp = NULL; } return 0; }
最新发布
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值