头插法创建链表实现插入操作结果不对,尾插法确是对的
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef int status;
typedef struct DouLnode
{
ElemType data;
struct DouLnode* next;
struct DouLnode* prior;
}DouLnode,*DouLinklist;
void InitDL(DouLinklist* L); //初始化双向链表
void hCreateDL(DouLinklist L, int length); //头插法创建
void tCreateDL(DouLinklist L, int length); //尾插发创建
status insertDL(DouLinklist L, int position, ElemType e); //插入
status deleteDL(DouLinklist L, int position, ElemType* e); //删除
void InitDL(DouLinklist* L)
{
*L = (DouLinklist)malloc(sizeof(DouLnode));
(*L)->next = NULL;
(*L)->prior = NULL;
}
void hCreateDL(DouLinklist L, int length)
{
printf("请输入%d个数据: \n", length);
for (int i = 0; i < length; i++)
{
DouLinklist p = (DouLinklist)malloc(sizeof(DouLnode));
ElemType data;
scanf("%d", &data);
p->data = data;
p->next = L->next;
p->prior = L;
L->next = p;
}
}
void tCreateDL(DouLinklist L, int length)
{
DouLinklist ptail = L;
printf("请输入%d个数据: \n", length);
for (int i = 0; i < length; i++)
{
DouLinklist p = (DouLinklist)malloc(sizeof(DouLnode));
ElemType data;
scanf("%d", &data);
p->data = data;
p->next = NULL;
p->prior = ptail;
ptail->next = p;
ptail = p;
}
}
status insertDL(DouLinklist L, int position, ElemType e)
{
DouLinklist s = (DouLinklist)malloc(sizeof(DouLnode));
s->data = e;
DouLinklist p = L;
int i = 0;
while (p && i < position)
{
p = p->next;
i++;
}
s->prior = p->prior;
p->prior->next = s;
s->next = p;
p->prior = s;
}
status deleteDL(DouLinklist L, int position, ElemType* e)
{
DouLinklist p = L;
int i = 0;
while (p && i < position)
{
p = p->next;
i++;
}
*e = p->data;
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
void traverseDL(DouLinklist L)
{
DouLinklist p = L->next;
while (p)
{
printf("%4d", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
DouLinklist L;
InitDL(&L);
//tCreateDL(L, 5);
hCreateDL(L, 6);
traverseDL(L);
insertDL(L, 2, 9);
traverseDL(L);
ElemType a;
deleteDL(L, 1, &a);
traverseDL(L);
return 0;
}
创建链表:1 2 3 4 5 6
遍历后打印出来是:6 5 4 3 2 1
插入后成了:9 5 4 3 2 1
本帖最后由 jackz007 于 2022-11-14 12:09 编辑
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType ;
typedef int status ;
typedef struct DouLnode
{
ElemType data ;
struct DouLnode* next;
struct DouLnode* prior ;
}DouLnode , * DouLinklist ;
void hCreateDL(DouLinklist * L , int length)
{
DouLinklist p1 , p2 ;
int i ;
printf("请输入%d个数据: \n", length) ;
for(i = 0 , p2 = NULL ; i < length ; i ++)
{
p1 = (DouLinklist) malloc(sizeof(DouLnode)) ;
ElemType data ;
scanf("%d", & data) ;
p1 -> data = data ;
p1 -> prior = NULL ;
if(! p2) {
p1 -> next = NULL ;
} else {
p1 -> next = p2 ;
p1 -> next -> prior = p1 ;
}
p2 = p1 ;
* L = p2 ;
}
}
void tCreateDL(DouLinklist * L, int length)
{
DouLinklist p1 , p2 ;
int i ;
printf("请输入%d个数据: \n", length) ;
for (i = 0 , p2 = NULL ; i < length ; i ++)
{
p1 = (DouLinklist) malloc(sizeof(DouLnode)) ;
ElemType data ;
scanf("%d", & data) ;
p1 -> data = data ;
p1 -> next = NULL ;
if(! p2) {
p1 -> prior = NULL ;
* L = p1 ;
} else {
p1 -> prior = p2 ;
p1 -> prior -> next = p1 ;
}
p2 = p1 ;
}
}
status insertDL(DouLinklist * L , int position , ElemType e)
{
int i ;
DouLinklist s = (DouLinklist) malloc(sizeof(DouLnode)) ;
s -> data = e ;
DouLinklist p ;
for(p = * L , i = 0 ; i < position ; i ++) p = p -> next ;
s -> next = p ;
s -> prior = p -> prior ;
s -> prior -> next = s ;
if(p == * L) * L = s ;
}
status deleteDL(DouLinklist * L, int position, ElemType * e)
{
DouLinklist p ;
int i ;
for(p = * L , i = 0 ; i < position ; i ++) p = p -> next ;
p -> prior -> next = p -> next ;
p -> next -> prior = p -> prior ;
* e = p -> data ;
if(* L == p) * L = p -> next ;
free(p) ;
}
void traverseDL(DouLinklist L)
{
DouLinklist p ;
for(p = L ; p ; p = p -> next) printf("%4d", p -> data);
printf("\n") ;
}
int main()
{
DouLinklist L = NULL ;
hCreateDL(& L , 6) ;
traverseDL(L) ;
insertDL(& L , 2 , 9) ;
traverseDL(L) ;
ElemType a ;
deleteDL(& L , 1 , & a) ;
traverseDL(L) ;
return 0 ;
}
编译运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
请输入6个数据:
1 2 3 4 5 6
6 5 4 3 2 1
6 5 9 4 3 2 1
6 9 4 3 2 1
D:\\C>
页:
[1]