1.请用编程实现双向链表的头插、头删、尾插、尾删
源代码:
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
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;i<index-1;i++)
p=p->next;
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;i<index-1;i++)
p=p->next;
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;i<index;i++)
p=p->next;
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;i<index;i++)
p=p->next;
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<len;i++)//循环输入数据域数据创建双向链表;
{
printf("%d element:",i+1);
scanf("%d",&element);
// head=insert_head(head,element);//头插
head=insert_rear(head,element);//尾插
}
output(head);//遍历数据域;
/* head=delete_head(head);
head=delete_head(head);
output(head);//头删两次后再次遍历;
head=delete_rear(head);
output(head);//尾删一次后再次遍历;
*/
int index;
/* printf("please enter index&insert key:");
scanf("%d %d",&index,&element);
head=insert_index(head,index,element);
output(head);//按位置插入后遍历;
printf("please enter index for delete:");
scanf("%d",&index);
head=delete_index(head,index);
output(head);//按位置删除后遍历;
printf("please enter index&change key:");
scanf("%d %d",&index,&element);
chg_index(head,index,element);
output(head);//按位置修改数值域后遍历;
*/
printf("please enter index for search:");
scanf("%d",&index);
search_index(head,index);
return 0;
}
效果显示:
2.请用编程实现双向链表按任意位置插入、删除、修改、查找
//头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
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<n;i++)
{
printf("please enter %d element:",i+1);
scanf(" %c",&element);
// head=inert_head(head,element);
head=inert_rear(head,element);
}
output(head);
//头删
// head=det_head(head);
// output(head);
//尾删
// head=det_rear(head);
// output(head);
//任意位置插入
int pos;
printf("please enter inert pos:");
scanf("%d",&pos);
printf("please enter inert element:");
scanf(" %c",&element);
head=inert_pos(head,pos,element);
output(head);
//任意位置删除
printf("please enter det pos:");
scanf("%d",&pos);
head=det_pos(head,pos);
output(head);
//查找
printf("please enter find pos:");
scanf("%d",&pos);
find_pos(head,pos);
//修改
printf("please enter cpy pos:");
scanf("%d",&pos);
printf("please enter cpy element:");
scanf(" %c",&element);
cpy_pos(head,pos,element);
output(head);
return 0;
}
//文本段
#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;i<pos-1;i++)
{
p=p->next;
}
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;i<pos-1;i++)
p=p->next;
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;i<pos-1;i++)
p=p->next;
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;i<pos-1;i++)
p=p->next;
p->data=element;
}
3.请简述栈和队列的区别
答:栈是只允许栈顶插入和删除的操作受限的线性表(先进后出);
队列是只允许在队尾插入、队头删除的线性表(先进先出);
4.请简述什么是内存泄露?
答:在释放空间时,指针位置并不在头部,指针之前的空间申请了内存空间,但没有释放,导致这部分内存无法再被其他程序使用。