作业要求
程序代码:
head.h:
#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 create();
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 delete_element(DoubleLink head,int pos,int n);
DoubleLink insert_element(DoubleLink head,int pos,int n,datatype element);
#endif
main.c:
#include "head.h"
int main(int argc, const char *argv[])
{
DoubleLink head =NULL;
int n;
datatype element;
printf("please input n:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("please input %d num:",i+1);
scanf(" %c",&element);
head=insert_head(head,element);//头插
// head=insert_rear(head,element);//尾插 //二选一即可
}
output(head);
head=delete_head(head);//头删
head=delete_rear(head);//尾删
int pos;
printf("please input delete pos:");
scanf("%d",&pos);
head=delete_element(head,pos,n);
printf("please input insert pos:");
scanf("%d",&pos);
printf("please input insert element:");
scanf(" %c",&element);
head=insert_element(head,pos,n,element);
output(head);
return 0;
}
test.c:
#include "head.h"
//创建
DoubleLink create()
{
DoubleLink s=(DoubleLink)malloc(sizeof(struct Node));
if(s==NULL)
return NULL;
s->data=0;
s->next=s->prev=s;
return s;
}
//头插
DoubleLink insert_head(DoubleLink head,datatype element)
{
//创建新节点
DoubleLink s=create();
s->data=element;
//判断链表是否为空
if(NULL==head)
head=s;
else
{
DoubleLink rear=head->prev;
s->next=head;
head->prev=s;
head=s;
head->prev=rear;
rear->next=head;
}
return head;
}
//尾插
DoubleLink insert_rear(DoubleLink head,datatype element)
{
DoubleLink s=create();
s->data=element;
DoubleLink p=head;
if(NULL==head)
head=s;
else
{
p=head->prev;
p->next=s;
s->prev=p;
s->next=head;
head->prev=s;
}
return head;
}
//循环遍历
void output(DoubleLink head)
{
//判断是否为空
if(NULL==head)
{
puts("empty");
return;
}
DoubleLink p=head;
//正向输出
while(p->next!=head)
{
printf("%c ",p->data);
p=p->next;
}
printf("%c ",p->data);
//逆向输出
// do
// {
// printf("%c ",p->data);
// p=p->prev;
// }while(p!=head->prev);
puts("");
}
//头删
DoubleLink delete_head(DoubleLink head)
{
if(NULL==head)
return head;
if(head->next==head)
{
free(head);
head=NULL;
return head;
}
else
{
DoubleLink del=head;
DoubleLink rear=head->prev;
head=del->next;
head->prev=rear;
rear->next=head;
free(del);
del=NULL;
return head;
}
}
//尾删
DoubleLink delete_rear(DoubleLink head)
{
if(NULL==head)
return head;
else if(head->next==head)
{
free(head);
head=NULL;
return head;
}
else
{
DoubleLink p=head->prev;
p->prev->next=head;
head->prev=p->prev;
free(p);
p=NULL;
return head;
}
}
//按任意位置删除
DoubleLink delete_element(DoubleLink head,int pos,int n)
{
if(NULL==head)
return head;
if(pos==1)
{
head=delete_head(head);
return head;
}
if(pos==n)
{
head=delete_rear(head);
return head;
}
int count=1;
DoubleLink p=head;
while(count!=pos)
{
p=p->next;
count++;
}
p->prev->next=p->next;
p->next->prev=p->prev;
free(p);
p=NULL;
return head;
}
//按任意位置插入
DoubleLink insert_element(DoubleLink head,int pos,int n,datatype element)
{
if(NULL==head)
return head;
if(pos==1)
{
head=insert_head(head,element);
return head;
}
if(pos==n)
{
head=insert_rear(head,element);
return head;
}
int count=1;
DoubleLink p=head;
DoubleLink s=create();
s->data=element;
while(count!=pos)
{
p=p->next;
count++;
}
s->next=p;
s->prev=p->prev;
p->prev->next=s;
p->prev=s;
return head;
}
插入运行结果:
删除运行结果:
课程总结: