1、双向链表的头插、头删、尾插、尾删
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
//数据域
datatype data;
//指针域
struct node *next;
struct node *pre;
}*Doublelist;
Doublelist create()
{
Doublelist s=(Doublelist)malloc(sizeof(struct node));
if(s==NULL)
return NULL;
s->data=0;
s->next=s->pre=NULL;
}
Doublelist head_insert(Doublelist head,datatype element)
{
Doublelist s=create();
s->data=element;
if(head==NULL)
head=s;
else
{
s->next=head;
head->pre=s;
head=s;
}
return head;
}
Doublelist rear_insert(Doublelist head,datatype element)
{
//创建节点
Doublelist s=create();
s->data=element;
Doublelist p=head;
if(head==NULL)
{
head=s;
return head;
}
while(p->next)
{
p=p->next;
}
p->next=s;
s->pre=p;
return head;
}
Doublelist head_delete(Doublelist head)
{
Doublelist del=head;
if(head==NULL)
return head;
if(del->next==NULL)
{
free(del);
del=NULL;
return head;
}
head=head->next;
head->pre=NULL;
free(del);
del=NULL;
return head;
}
Doublelist rear_delete(Doublelist head)
{
if(head==NULL)
return head;
if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
Doublelist p=head;
while(p->next)
{
p=p->next;
}
p->pre->next=NULL;
free(p);
p=NULL;
return head;
}
void output(Doublelist head)
{
Doublelist p=head;
if(head==NULL)
{
puts("empty!");
return;
}
while(p->next)
{
printf("%-5d",p->data);
p=p->next;
}
printf("%-5d",p->data);
puts("");
return;
}
int main(int argc, const char *argv[])
{
int n;
datatype element;
Doublelist head=NULL;
printf("please enter n:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("please enter %dth num:",i+1);
scanf("%d",&element);
//头插
//head=head_insert(head,element);
//尾插
head=rear_insert(head,element);
}
output(head);
//头删
head=head_delete(head);
output(head);
//尾删
head=rear_delete(head);
output(head);
return 0;
}
2、双向链表任意位置的插入、删除、修改、查找
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
enum{FALSE=-1,SUCCESS};
typedef int datatype;
typedef struct node
{
//数据域
datatype data;
//指针域
struct node *next;
struct node *pre;
}*Doublelist;
Doublelist create()
{
Doublelist s=(Doublelist)malloc(sizeof(struct node));
if(s==NULL)
return NULL;
s->data=0;
s->next=s->pre=NULL;
}
Doublelist head_insert(Doublelist head,datatype element)
{
Doublelist s=create();
s->data=element;
if(head==NULL)
head=s;
else
{
s->next=head;
head->pre=s;
head=s;
}
return head;
}
Doublelist rear_insert(Doublelist head,datatype element)
{
//创建节点
Doublelist s=create();
s->data=element;
Doublelist p=head;
if(head==NULL)
{
head=s;
return head;
}
while(p->next)
{
p=p->next;
}
p->next=s;
s->pre=p;
return head;
}
Doublelist head_delete(Doublelist head)
{
Doublelist del=head;
if(head==NULL)
return head;
if(del->next==NULL)
{
free(del);
del=NULL;
return head;
}
head=head->next;
head->pre=NULL;
free(del);
del=NULL;
return head;
}
Doublelist rear_delete(Doublelist head)
{
if(head==NULL)
return head;
if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
Doublelist p=head;
while(p->next)
{
p=p->next;
}
p->pre->next=NULL;
free(p);
p=NULL;
return head;
}
void output(Doublelist head)
{
Doublelist p=head;
if(head==NULL)
{
puts("empty!");
return;
}
while(p->next)
{
printf("%-5d",p->data);
p=p->next;
}
printf("%-5d",p->data);
puts("");
return;
}
int length(Doublelist head)
{
int len=0;
Doublelist p=head;
while(p)
{
p=p->next;
len++;
}
return len;
}
Doublelist any_insert(Doublelist head,int pos,datatype element)
{
int len=length(head);
if(pos<1||pos>len+1)
{
puts("pos error");
return head;
}
Doublelist s=create();
s->data=element;
if(pos==1)
{
head=head_insert(head,element);
return head;
}
Doublelist p=head;
for(int i=1;i<pos-1;i++)
{
p=p->next;
}
s->next=p->next;
s->pre=p;
p->next->pre=s;
s->pre=p;
return head;
}
Doublelist any_delete(Doublelist head,int pos)
{
datatype len=length(head);
if(pos<1||pos>len||head==NULL)
{
puts("pos error");
return head;
}
if(pos==1)
{
head=head_delete(head);
return head;
}
Doublelist p=head;
for(int i=1;i<pos-1;i++)
{
p=p->next;
}
Doublelist del=p->next;
p->next=del->next;
del->next->pre=p;
free(del);
del=NULL;
return head;
}
void any_change(Doublelist head,int pos,datatype element)
{
int len=length(head);
if(pos<1||pos>len||head==NULL)
{
puts("error");
return ;
}
Doublelist p=head;
for(int i=1;i<pos;i++)
p=p->next;
p->data=element;
return;
}
int pos_search(Doublelist head,int pos)
{
int len=length(head);
Doublelist p=head;
if(pos<1||pos>len||head==NULL)
{
puts("pos error");
return FALSE;
}
for(int i=1;i<pos;i++)
p=p->next;
return p->data;
}
int main(int argc, const char *argv[])
{
int n;
datatype element;
Doublelist head=NULL;
printf("please enter n:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("please enter %dth num:",i+1);
scanf("%d",&element);
//头插
//head=head_insert(head,element);
//尾插
head=rear_insert(head,element);
}
output(head);
//头删
//head=head_delete(head);
//output(head);
//尾删
//head=rear_delete(head);
//output(head);
//任意位置插入
int pos;
printf("please enter the pos:");
scanf("%d",&pos);
printf("please enter the insert element:");
scanf("%d",&element);
head=any_insert(head,pos,element);
output(head);
//任意位置删除
printf("please enter the delete pos:");
scanf("%d",&pos);
head=any_delete(head,pos);
printf("the new list is:");
output(head);
//按任意位置修改
printf("please enter the amend pos:");
scanf("%d",&pos);
printf("please enter the new num:");
scanf("%d",&element);
any_change(head,pos,element);
output(head);
//按任意位置查找
printf("please enter the search pos:");
scanf("%d",&pos);
element=pos_search(head,pos);
printf("the num you search is:%d\n",element);
return 0;
}
3、说明栈和队列的区别
栈和队列的区别:栈是先进后出,可以在一端进行操作,逻辑连续物理不一定连续,队列是先进先出,在两端操作,内存连续
4、说明什么是内存泄露
内存泄漏:在释放的时候,指针没有指向首地址而是指向了中间的某一块地址