1.请编程实现双向链表的头插,头删、尾插、尾删
头插:
尾插:
头删:
尾删:
2.请编程实现双向链表按任意位置插入、删除、修改、查找
插入:
删除:
查找:
修改:
头文件:
#ifndef __HEAD_H_
#define __HEAD_H_
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int datatype;
enum {FALSE=-1,SUCCESS};
typedef struct node
{
datatype data;
struct node *next;
struct node *prev;
}*doublelink;
doublelink creat();
doublelink insert_head(doublelink head,datatype element);
doublelink insert_rear(doublelink head,datatype element);
void output(doublelink head);
doublelink delete_head(doublelink head);
doublelink delete_rear(doublelink head);
doublelink insert_pos(doublelink head,int pos,datatype element);
doublelink delete_pos(doublelink head,int pos);
datatype search_pos(doublelink head,int pos);
void update_pos(doublelink head,int pos,datatype element);
#endif
主函数:
#include"head.h"
int main(int argc, const char *argv[])
{
doublelink head=NULL;
int n;
printf("please enter n:");
scanf("%d",&n);
datatype element;
for(int i=0;i<n;i++)
{
printf("please enter %d element:",i+1);
scanf("%d",&element);
//头插
// head=insert_head(head,element);
//尾插
head=insert_rear(head,element);
}
//遍历输出
output(head);
//头删
// head=delete_head(head);
// output(head);
//尾删
// head=delete_rear(head);
// output(head);
//任意位置插入
int pos;
printf("please enter pos:");
scanf("%d",&pos);
printf("please enter element:");
scanf("%d",&element);
// head=insert_pos(head,pos,element);
// output(head);
//任意位置删除
// head=delete_pos(head,pos);
// output(head);
//任意位置查找
// datatype s=search_pos(head,pos);
// if(s==-1)
// puts("no");
// else
// printf("s=%d\n",s);
//任意位置修改
update_pos(head,pos,element);
output(head);
return 0;
}
自定义函数:
#include"head.h"
//创建节点
doublelink creat()
{
doublelink head=(doublelink)malloc(sizeof(struct node));
if(head==NULL)
return NULL;
head->data=0;
head->next=head->prev=NULL;
return head;
}
//头插
doublelink insert_head(doublelink head,datatype element)
{
doublelink s=creat();
s->data=element;
if(head==NULL)
{
head=s;
}
else
{
s->next=head;
head->prev=s;
head=s;
}
return head;
}
//尾插
doublelink insert_rear(doublelink head,datatype element)
{
doublelink s=creat();
s->data=element;
if(NULL==head)
head=s;
else
{
doublelink p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=s;
s->prev=p;
}
return head;
}
//输出
void output(doublelink head)
{
if(head==NULL)
{
printf("empty\n");
return;
}
doublelink p=head;
while(p->next)
{
printf("%-4d",p->data);
p=p->next;
}
printf("%-4d\n",p->data);
}
//头删
doublelink delete_head(doublelink head)
{
if(head==NULL)
return head;
/* if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
*/
doublelink del=head;
head=head->next;
head->prev=NULL;
free(del);
del=NULL;
return head;
}
//尾删
doublelink delete_rear(doublelink head)
{
if(head==NULL)
return head;
if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
doublelink p=head;
while(p->next)
{
p=p->next;
}
p->prev->next=NULL;
free(p);
p=NULL;
return head;
}
//计算长度
int length(doublelink head)
{
int len=0;
doublelink p=head;
while(p)
{
len++;
p=p->next;
}
return len;
}
/*
* function: 任意位置插入
* @param [ in]
* @param [out] 头,位置,插入元素
* @return 头
*/
doublelink insert_pos(doublelink head,int pos,datatype element)
{
if(pos==1)
{
head=insert_head(head,element);
return head;
}
else if(pos>1 || pos<=length(head))
{
doublelink s=creat();
s->data=element;
doublelink p=head;
for(int i=1;i<pos-1;i++)
{
p=p->next;
}
s->next=p->next;
s->prev=p;
p->next->prev=s;
p->next=s;
return head;
}
else if(pos==length(head)+1)
{
head=insert_rear(head,element);
return head;
}
else
{
puts("error");
return head;
}
}
//任意位置删除
doublelink delete_pos(doublelink head,int pos)
{
if(head==NULL || pos<1 || pos>length(head))
{
puts("error");
return head;
}
if(pos==1)
{
head=delete_head(head);
}
else if(pos>1 && pos<length(head))
{
doublelink p=head;
doublelink del=head;
for(int i=1;i<pos-1;i++)
{
p=p->next;
}
del=p->next;
p->next=del->next;
del->next->prev=p;
free(del);
del=NULL;
return head;
}
else if(pos==length(head))
{
head=delete_rear(head);
return head;
}
}
//任意位置查找
datatype search_pos(doublelink head,int pos)
{
if(head==NULL || pos<1 || pos>length(head))
{
return FALSE;
}
doublelink p=head;
int i=1;
for(i;i<pos;i++)
{
p=p->next;
}
return p->data;
}
//任意位置修改
void update_pos(doublelink head,int pos,datatype element)
{
if(head==NULL || pos<1 || pos>length(head))
{
puts("error");
return;
}
doublelink p=head;
int i=1;
for(i;i<pos;i++)
{
p=p->next;
}
p->data=element;
}
3、栈和队列的区别
栈:先进后出;在栈顶端实现插入删除;
队列:先进先出;在队尾插入,队首删除;
4、什么是内存泄漏
在释放内存时,头指针没有指向堆区的首地址,会造成内存泄漏