1.请用编程实现双向链表的头插、头删、尾插、尾删
源代码:
#include
#include
#include
typedef int datatype;
typedef struct Node//定义双向链表的节点结构体
{
datatype data;//数据域
struct Node *prev;//指针域 指向上个节点
struct Node *next;//指针域 指向下个节点
}*doublelink;
doublelink create_node()
{
doublelink s=(doublelink)malloc(sizeof(struct Node));
if(NULL==s)
return NULL;
s->data=0;
s->prev=NULL;
s->next=NULL;
return s;
}
doublelink insert_head(doublelink head,datatype element)
{
doublelink s=create_node();
if(NULL==s)
{
puts("create_node fail");
return NULL;
}
s->data=element;
if(head==NULL)
{
head=s;
return head;
}
s->next=head;
head->prev=head;
head=s;
return head;
}
doublelink insert_rear(doublelink head,datatype element)
{
doublelink s=create_node();
if(NULL==s)
{
puts("create_node fail");
return NULL;
}
s->data=element;
if(NULL==head)
{
head=s;
return head;
}
doublelink p=head;
while(p->next)
p=p->next;
p->next=s;
s->prev=p;
return head;
}
doublelink delete_head(doublelink head)
{
if(head==NULL)
{
puts("empty");
return NULL;
}
if(NULL==head->next)
{
free(head);
return NULL;
}
doublelink del=head;
head=head->next;
head->prev=NULL;
free(del);del=NULL;
return head;
}
doublelink delete_rear(doublelink head)
{
if(head==NULL)
{
puts("empty");
return NULL;
}
if(NULL==head->next)
{
free(head);
return NULL;
}
doublelink p=head;
while(p->next->next)
p=p->next;
doublelink del=p->next;
p->next=NULL;
free(del);del=NULL;
return head;
}
int lengh(doublelink head)
{
int l=0;
while(head)
{
l++;
head=head->next;
}
return l;
}
doublelink insert_index(doublelink head,int index,datatype element)
{
int l=lengh(head);
if(NULL==head || index<1 || index>l+1)
{
puts("empty or index error");
return head;
}
if(1==index)
{
head=insert_head(head,element);
return head;
}
if(l+1==index)
{
head=insert_rear(head,element);
return head;
}
doublelink s=create_node();
s->data=element;
doublelink p=head;
for(int i=1;inext;
s->next=p->next;
s->prev=p;
p->next->prev=s;
p->next=s;
return head;
}
doublelink delete_index(doublelink head,int index)
{
if(NULL==head || index<1 || index>lengh(head))
{
puts("empty or index error");
return head;
}
if(1==index)
{
head=delete_head(head);
return head;
}
if(index==lengh(head))
{
head=delete_rear(head);
return head;
}
doublelink p=head;
for(int i=1;inext;
doublelink del=p->next;
p->next=del->next;
del->next->prev=p;
free(del);del=NULL;
return head;
}
void chg_index(doublelink head,int index,datatype element)
{
if(NULL==head || index<1 || index>lengh(head))
{
puts("empty or index error");
return;
}
doublelink p=head;
for(int i=1;inext;
p->data=element;
}
void search_index(doublelink head,int index)
{
if(NULL==head || index<1 || index>lengh(head))
{
puts("empty or index error");
return;
}
doublelink p=head;
for(int i=1;inext;
printf("search_index is: %d\n",p->data);
}
void output(doublelink head)
{
while(head)
{
printf("%-4d",head->data);
head=head->next;
}
puts("");
}
int main(int argc, const char *argv[])
{
doublelink head=NULL;
int len;
printf("please enter the lengh of the doublelink:");
scanf("%d",&len);
datatype element;
for(int i=0;i
效果显示:
2.请用编程实现双向链表按任意位置插入、删除、修改、查找
//头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include
#include
#include
typedef char datatype;
//定义双向链表结构体
typedef struct Node
{
//数据域:存储数据元素
datatype data;
//指针域:下一个节点的地址
struct Node *next;
//指针域:上一个节点的地址
struct Node *prev;
}*Doublelink;
Doublelink inert_head(Doublelink head,datatype element);
Doublelink creat();
void output(Doublelink head);
Doublelink inert_rear(Doublelink head,datatype element);
Doublelink det_head(Doublelink head);
Doublelink det_rear(Doublelink head);
int length(Doublelink head);
Doublelink inert_pos(Doublelink head,int pos,datatype element);
Doublelink det_pos(Doublelink head,int pos);
void find_pos(Doublelink head,int pos);
void cpy_pos(Doublelink head,int pos,datatype element);
#endif
//主函数
#include"head.h"
int main(int argc, const char *argv[])
{
Doublelink head=NULL;
datatype element;
int n;
printf("please enter n:");
scanf("%d",&n);
for(int i=0;i
//文本段
#include"head.h"
/*
* function: 创建新节点
* @param [ in]
* @param [out]
* @return 成功返回s 失败返回NULL
*/
Doublelink creat()
{
Doublelink s=(Doublelink)malloc(sizeof(struct Node));
if(s==NULL)
return NULL;
s->data=0;
s->next=s->prev=NULL;
return s;
}
/*
* function: 头插
* @param [ in]
* @param [out] head,element
* @return head
*/
Doublelink inert_head(Doublelink head,datatype element)
{
Doublelink s=creat();
s->data=element;
if(NULL==head)
head=s;
else
{
s->next=head;
head->prev=s;
head=s;
}
return head;
}
/*
* function: 遍历输出
* @param [ in]
* @param [out] head
* @return head
*/
void output(Doublelink head)
{
if(NULL==head)
{
puts("empty");
return;
}
else
{
Doublelink p=head;
while(p->next!=NULL)//p到倒数第二个节点
{
printf("%-5c",p->data);
p=p->next;
}
printf("%-5c\n",p->data);//最后一个元素输出
/* while(p)//逆向输出
{
printf("%-5c",p->data);
p=p->prev;
}
*/
// puts("");
return;
}
}
/*
* function: 尾插
* @param [ in]
* @param [out] head,element
* @return head
*/
Doublelink inert_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;
}
/*
* function: 头删
* @param [ in]
* @param [out] head
* @return head
*/
Doublelink det_head(Doublelink head)
{
if(NULL==head)
return head;
//多个节点>=1
Doublelink p=head;
head=p->next;
head->prev=NULL;
free(p);
p=NULL;
return head;
}
/*
* function: 尾删
* @param [ in]
* @param [out] head
* @return head
*/
Doublelink det_rear(Doublelink head)
{
if(NULL==head)
return head;
else if(head->next==NULL)//一个节点
{
free(head);
head=NULL;
return head;
}
else//多个节点>=2
{
Doublelink p=head;
while(p->next)
p=p->next;
p->prev->next=NULL;
free(p);
p=NULL;
return head;
}
}
/*
* function: 计算链表长度
* @param [ in]
* @param [out] head
* @return len
*/
int length(Doublelink head)
{
int len=0;
while(head)
// printf("%-5c\n",p->data);//最后一个元素输出 puts("");
{
len++;
head=head->next;
}
return len;
}
/*
* function: 任意位置插入
* @param [ in]
* @param [out] head pos
* @return head
*/
Doublelink inert_pos(Doublelink head,int pos,datatype element)
{
int len=length(head);
Doublelink s=creat();
s->data=element;
if(NULL==head||pos<1||pos>len+1)
{
puts("error");
return NULL;
}
if(pos==1)
{
head=inert_head(head,element);
return head;
}
Doublelink p=head;
for(int i=0;inext;
}
Doublelink k=p->prev;
k->next=s;
s->prev=k;
s->next=p;
p->prev=s;
return head;
}
/*
* function: 任意位置删除
* @param [ in]
* @param [out] head pos
* @return head
*/
Doublelink det_pos(Doublelink head,int pos)
{
int len=length(head);
//printf("%d\n",len);
if(NULL==head||pos<1||pos>len)
{
puts("error");
return NULL;
}
if(pos==1)
{
head=det_head(head);
return head;
}
else if(pos==len)
{
head=det_rear(head);
return head;
}
Doublelink p=head;
for(int i=0;inext;
Doublelink k=p->prev;
k->next=p->next;
p->next->prev=k;
free(p);
p=NULL;
return head;
}
/*
* function: 逆置
* @param [ in]
* @param [out] head
* @return head
*/
Doublelink rever(Doublelink head)
{
if(NULL==head)
return head;
Doublelink p=head->next;
head->next=NULL;
p->prev=NULL;
while(p)
{
Doublelink t=p;
p=p->next;
t->next=head;
head->prev=t;
t->prev=NULL;
head=t;
}
return head;
}
void find_pos(Doublelink head,int pos)
{
int len=length(head);
if(NULL==head||pos<1||pos>len)
return ;
Doublelink p=head;
for(int i=0;inext;
printf("%c\n",p->data);
}
void cpy_pos(Doublelink head,int pos,datatype element)
{
int len=length(head);
if(NULL==head||pos<1||pos>len)
return;
Doublelink p=head;
for(int i=0;inext;
p->data=element;
}
3.请简述栈和队列的区别
答:栈是只允许栈顶插入和删除的操作受限的线性表(先进后出);
队列是只允许在队尾插入、队头删除的线性表(先进先出);
4.请简述什么是内存泄露?
答:在释放空间时,指针位置并不在头部,指针之前的空间申请了内存空间,但没有释放,导致这部分内存无法再被其他程序使用。