源代码:
#include <stdio.h>
#include <stdlib.h>
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;i<pos;i++)
{
p=p->next;
}
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;i<pos;i++)
{
p=p->next;
}
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;i<pos;i++)
{
p=p->next;
}
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;i<pos;i++)
{
p=p->next;
}
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<n;i++)
{
printf("please enter %d element : ",i+1 );
scanf("%d",&element);
//head=head_insert(head,element);
head=rear_insert(head,element);
}
head=head_delete(head);
head=rear_delete(head);
output(head);
printf("please enter insert pos : ");
scanf("%d",&pos);
printf("please enter insert element : ");
scanf("%d",&element);
insert_pos(head,pos,element);
printf("please enter delete pos : ");
scanf("%d",&pos);
head=delete_pos(head,pos);
printf("please enter update pos : ");
scanf("%d",&pos);
printf("please enter update element : ");
scanf("%d",&element);
update_pos(head,pos,element);
printf("please enter search pos : ");
scanf("%d",&pos);
search_pos(head,pos);
output(head);
return 0;
}
1>
要求:
效果图:
2>
要求:
效果图:
3>
要求:
答:
(1)栈中定义的变量符合先进后出,队列则符合先进先出
(2)栈中只能从对栈顶操作,队列可以对队头、队尾操作
4>
要求:
答:
在堆区动态申请内存后,释放内存时未将释放指针指向堆区首地址,导致内存无法回收成功。