2024-2-4-复习作业

2024-2-4-复习作业_第1张图片

源代码:

#include 
#include 
typedef int datatype; 
typedef struct Node
{
    datatype data;
    struct Node *next;
    struct Node *prev;
}*DoubleLinkList;


DoubleLinkList create()
{
    DoubleLinkList s=(DoubleLinkList)malloc(sizeof(struct Node));
    s->data=0;
    s->next=s->prev=NULL;
    return s;
}
DoubleLinkList head_insert(DoubleLinkList head ,int element)
{
    DoubleLinkList s=create();
        s->data=element;
    if(head==NULL) 
        head=s;
    else
    {
        s->next=head;
        head->prev=s;
        head=s;
    }

    return head;
}

DoubleLinkList rear_insert(DoubleLinkList head,int element)
{
    DoubleLinkList s=create();
    s->data=element;
    if(head==NULL)  head=s;
    else
    {
        DoubleLinkList p=head;
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=s;
        s->prev=p;
    }
        return head;
}
DoubleLinkList head_delete(DoubleLinkList head)
{
    if(head==NULL) return NULL;
    DoubleLinkList del=head;
    head=head->next;
    head->prev=NULL;
    free(del);del=NULL;
    return head;
    }

DoubleLinkList rear_delete(DoubleLinkList head)
{
    if(head==NULL) return NULL;
    
    if(head->next==NULL)
    {
        free(head);
        head=NULL;
        return head;
    }    
    DoubleLinkList p=head;
    while(p->next)
    {
        p=p->next;
    }
    p->prev->next=NULL;
    free(p);
    p=NULL;
    return head;    
}

void output(DoubleLinkList head)
{

    if(head==NULL) { printf("empty\n"); return;}
    DoubleLinkList p=head;
    while (p->next!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("%d \n",p->data);
}
int length(DoubleLinkList head)
{
    int len=0;
    if(head==NULL) return len;
    while(head!=NULL)
    {
        head=head->next;
        len++;
    }
    return len;
}
void insert_pos(DoubleLinkList head,int pos,int element)
{
    int len=length(head);
    //printf("len=%d\n",len);
    if( len==0 || pos<=0 || pos>len ) {puts("error pos !"); return;}
    DoubleLinkList p=head;
    for(int i=1;inext;
    }
    DoubleLinkList s=create();
    s->data=element;
    datatype t=p->data;
    p->data=s->data;
    s->data=t;
    if(p->next==NULL) 
    {
        p=p->prev;
    }
    s->next=p->next;
    s->prev=p;

    p->next->prev=s;
    p->next=s;
}

DoubleLinkList delete_pos(DoubleLinkList head,int pos)
{
    int len=length(head);
        if( len==0 || pos<=0 || pos>len ) {puts("error pos !"); return NULL;}
        if(pos==1)
        {
            head=head_delete(head);
            return head;
        }
    DoubleLinkList p=head;
    for(int i=1;inext;
    }
    if(pos==len)
    {
        head=rear_delete(head);
        return head;
    }
    DoubleLinkList del=p;
    p->prev->next=p->next;
    p->next->prev=p->prev;
    free(del);del=NULL;
    return head;

}
void update_pos(DoubleLinkList head,int pos,int element)
{
    
    if(pos>length(head) || pos<=0 || head==NULL)
    {
        puts("error pos !");
        return ;   
    }
    DoubleLinkList p=head;
    for(int i=1;inext;
    }
    p->data=element;   

}

void search_pos(DoubleLinkList head,int pos)
{
    if(pos>length(head) || pos<=0 || head==NULL)
    {
        puts("error pos !");
        return ;   
    }
    DoubleLinkList p=head;
    for(int i=1;inext;
    }
    printf("The search element is %d \n",p->data);
}

int main(void) 
{
    int n,element,pos;
    DoubleLinkList head=NULL;
    printf("please enter n : " );
    scanf("%d",&n);
    for(int i=0;i

1>

要求:

效果图:

2024-2-4-复习作业_第2张图片

2024-2-4-复习作业_第3张图片

2024-2-4-复习作业_第4张图片

2024-2-4-复习作业_第5张图片

2>

要求:

效果图:

2024-2-4-复习作业_第6张图片

2024-2-4-复习作业_第7张图片

2024-2-4-复习作业_第8张图片

2024-2-4-复习作业_第9张图片

3>

要求:

答:

(1)栈中定义的变量符合先进后出,队列则符合先进先出

(2)栈中只能从对栈顶操作,队列可以对队头、队尾操作

4>

要求:

答:

在堆区动态申请内存后,释放内存时未将释放指针指向堆区首地址,导致内存无法回收成功。

你可能感兴趣的:(linux,c语言,开发语言,数据结构,算法)