数据结构 exercise3-循环链表、双链表
1.p74实验三2.p74实验四3.删除循环单链表中的奇数4.双链表降序排列
1.p74实验三
#include <stdio.h>
#include <malloc.h>
typedef int ElemType
;
typedef struct DNode
{
ElemType data
;
struct DNode
*prior
;
struct DNode
*next
;
} DLinkNode
;
void CreateListF(DLinkNode
*&L
,ElemType a
[],int n
)
{
DLinkNode
*s
;
L
=(DLinkNode
*)malloc(sizeof(DLinkNode
));
L
->prior
=L
->next
=NULL;
for (int i
=0;i
<n
;i
++)
{
s
=(DLinkNode
*)malloc(sizeof(DLinkNode
));
s
->data
=a
[i
];
s
->next
=L
->next
;
if (L
->next
!=NULL) L
->next
->prior
=s
;
L
->next
=s
;s
->prior
=L
;
}
}
void CreateListR(DLinkNode
*&L
,ElemType a
[],int n
)
{
DLinkNode
*s
,*r
;
L
=(DLinkNode
*)malloc(sizeof(DLinkNode
));
L
->prior
=L
->next
=NULL;
r
=L
;
for (int i
=0;i
<n
;i
++)
{
s
=(DLinkNode
*)malloc(sizeof(DLinkNode
));
s
->data
=a
[i
];
r
->next
=s
;s
->prior
=r
;
r
=s
;
}
r
->next
=NULL;
}
void InitList(DLinkNode
*&L
)
{
L
=(DLinkNode
*)malloc(sizeof(DLinkNode
));
L
->prior
=L
->next
=NULL;
}
void DestroyList(DLinkNode
*&L
)
{
DLinkNode
*pre
=L
,*p
=pre
->next
;
while (p
!=NULL)
{
free(pre
);
pre
=p
;
p
=pre
->next
;
}
free(pre
);
}
bool ListEmpty(DLinkNode
*L
)
{
return(L
->next
==NULL);
}
int ListLength(DLinkNode
*L
)
{
DLinkNode
*p
=L
;
int i
=0;
while (p
->next
!=NULL)
{
i
++;
p
=p
->next
;
}
return(i
);
}
void DispList(DLinkNode
*L
)
{
DLinkNode
*p
=L
->next
;
while (p
!=NULL)
{
printf("%c ",p
->data
);
p
=p
->next
;
}
printf("\n");
}
bool GetElem(DLinkNode
*L
,int i
,ElemType
&e
)
{
int j
=0;
DLinkNode
*p
=L
;
if (i
<=0) return false;
while (j
<i
&& p
!=NULL)
{
j
++;
p
=p
->next
;
}
if (p
==NULL)
return false;
else
{
e
=p
->data
;
return true;
}
}
int LocateElem(DLinkNode
*L
,ElemType e
)
{
int n
=1;
DLinkNode
*p
=L
->next
;
while (p
!=NULL && p
->data
!=e
)
{
n
++;
p
=p
->next
;
}
if (p
==NULL)
return(0);
else
return(n
);
}
bool ListInsert(DLinkNode
*&L
,int i
,ElemType e
)
{
int j
=0;
DLinkNode
*p
=L
,*s
;
if (i
<=0) return false;
while (j
<i
-1 && p
!=NULL)
{
j
++;
p
=p
->next
;
}
if (p
==NULL)
return false;
else
{
s
=(DLinkNode
*)malloc(sizeof(DLinkNode
));
s
->data
=e
;
s
->next
=p
->next
;
if (p
->next
!=NULL)
p
->next
->prior
=s
;
s
->prior
=p
;
p
->next
=s
;
return true;
}
}
bool ListDelete(DLinkNode
*&L
,int i
,ElemType
&e
)
{
int j
=0;
DLinkNode
*p
=L
,*q
;
if (i
<=0) return false;
while (j
<i
-1 && p
!=NULL)
{
j
++;
p
=p
->next
;
}
if (p
==NULL)
return false;
else
{
q
=p
->next
;
if (q
==NULL)
return false;
e
=q
->data
;
p
->next
=q
->next
;
if (p
->next
!=NULL) p
->next
->prior
=p
;
free(q
);
return true;
}
}
int main()
{
DLinkNode
*h
;
ElemType e
;
printf("双链表的基本运算如下:\n");
printf("1、初始化双链表h\n");
InitList(h
);
printf("2、依次采用尾插法插入a,b,c,d,e元素\n");
ListInsert(h
,1,'a');
ListInsert(h
,2,'b');
ListInsert(h
,3,'c');
ListInsert(h
,4,'d');
ListInsert(h
,5,'e');
printf("3、输出双链表h:");
DispList(h
);
printf("4、双链表h的长度:%d\n",ListLength(h
));
printf("5、双链表h为%s\n",(ListLength(h
)?"空":"非空"));
GetElem(h
,3,e
);
printf("6、双链表h的第三个元素是:%c\n",e
);
printf("7、元素a的位置:%d\n",LocateElem(h
,'a'));
printf("8、在第4个元素位置上插入f元素\n");
ListInsert(h
,4,'f');
printf("9、输出双链表h:");
DispList(h
);
printf("10、删除h的第三个元素\n");
ListDelete(h
,3,e
);
printf("11、输出双链表h:");
DispList(h
);
printf("12、释放双链表h\n");
DestroyList(h
);
return 1;
}
2.p74实验四
#include <stdio.h>
#include <malloc.h>
typedef int ElemType
;
typedef struct LNode
{
ElemType data
;
struct LNode
*next
;
} LinkNode
;
void CreateListF(LinkNode
*&L
,ElemType a
[],int n
)
{
LinkNode
*s
;int i
;
L
=(LinkNode
*)malloc(sizeof(LinkNode
));
L
->next
=NULL;
for (i
=0;i
<n
;i
++)
{
s
=(LinkNode
*)malloc(sizeof(LinkNode
));
s
->data
=a
[i
];
s
->next
=L
->next
;
L
->next
=s
;
}
s
=L
->next
;
while (s
->next
!=NULL)
s
=s
->next
;
s
->next
=L
;
}
void CreateListR(LinkNode
*&L
,ElemType a
[],int n
)
{
LinkNode
*s
,*r
;int i
;
L
=(LinkNode
*)malloc(sizeof(LinkNode
));
L
->next
=NULL;
r
=L
;
for (i
=0;i
<n
;i
++)
{
s
=(LinkNode
*)malloc(sizeof(LinkNode
));
s
->data
=a
[i
];
r
->next
=s
;
r
=s
;
}
r
->next
=L
;
}
void InitList(LinkNode
*&L
)
{
L
=(LinkNode
*)malloc(sizeof(LinkNode
));
L
->next
=L
;
}
void DestroyList(LinkNode
*&L
)
{
LinkNode
*p
=L
,*q
=p
->next
;
while (q
!=L
)
{
free(p
);
p
=q
;
q
=p
->next
;
}
free(p
);
}
bool ListEmpty(LinkNode
*L
)
{
return(L
->next
==L
);
}
int ListLength(LinkNode
*L
)
{
LinkNode
*p
=L
;int i
=0;
while (p
->next
!=L
)
{
i
++;
p
=p
->next
;
}
return(i
);
}
void DispList(LinkNode
*L
)
{
LinkNode
*p
=L
->next
;
while (p
!=L
)
{
printf("%c ",p
->data
);
p
=p
->next
;
}
printf("\n");
}
bool GetElem(LinkNode
*L
,int i
,ElemType
&e
)
{
int j
=0;
LinkNode
*p
;
if (L
->next
!=L
)
{
if (i
==1)
{
e
=L
->next
->data
;
return true;
}
else
{
p
=L
->next
;
while (j
<i
-1 && p
!=L
)
{
j
++;
p
=p
->next
;
}
if (p
==L
)
return false;
else
{
e
=p
->data
;
return true;
}
}
}
else
return false;
}
int LocateElem(LinkNode
*L
,ElemType e
)
{
LinkNode
*p
=L
->next
;
int n
=1;
while (p
!=L
&& p
->data
!=e
)
{
p
=p
->next
;
n
++;
}
if (p
==L
)
return(0);
else
return(n
);
}
bool ListInsert(LinkNode
*&L
,int i
,ElemType e
)
{
int j
=0;
LinkNode
*p
=L
,*s
;
if (p
->next
==L
|| i
==1)
{
s
=(LinkNode
*)malloc(sizeof(LinkNode
));
s
->data
=e
;
s
->next
=p
->next
;
p
->next
=s
;
return true;
}
else
{
p
=L
->next
;
while (j
<i
-2 && p
!=L
)
{
j
++;
p
=p
->next
;
}
if (p
==L
)
return false;
else
{
s
=(LinkNode
*)malloc(sizeof(LinkNode
));
s
->data
=e
;
s
->next
=p
->next
;
p
->next
=s
;
return true;
}
}
}
bool ListDelete(LinkNode
*&L
,int i
,ElemType
&e
)
{
int j
=0;
LinkNode
*p
=L
,*q
;
if (p
->next
!=L
)
{
if (i
==1)
{
q
=L
->next
;
e
=q
->data
;
L
->next
=q
->next
;
free(q
);
return true;
}
else
{
p
=L
->next
;
while (j
<i
-2 && p
!=L
)
{
j
++;
p
=p
->next
;
}
if (p
==L
)
return false;
else
{
q
=p
->next
;
e
=q
->data
;
p
->next
=q
->next
;
free(q
);
return true;
}
}
}
else return false;
}
int main()
{
LinkNode
*h
;
ElemType e
;
printf("循环单链表的基本运算如下:\n") ;
printf("(1)初始化循环单链表h\n") ;
InitList(h
) ;
printf("(2)依次采用尾插法插入a,b,c,d,e元素\n") ;
ListInsert(h
,1,'a') ;
ListInsert(h
,2,'b') ;
ListInsert(h
,3,'c') ;
ListInsert(h
,4,'d') ;
ListInsert(h
,5,'e') ;
printf("(3)输出循环单链表h:");
DispList(h
) ;
printf("(4)循环单链表h长度:%d\n",ListLength(h
));
printf("(5)循环单链表h为%s\n",(ListEmpty(h
)?"空":"非空"));
printf("(6)循环单链表h的第三个元素:%c\n",e
) ;
printf("(7)元素a的位置:%d\n",LocateElem(h
,'a')) ;
printf("(8)在第四个元素位置上插入f元素\n") ;
ListInsert(h
,4,'f') ;
printf("(9)输出循环单链表h:");
DispList(h
) ;
printf("(10)删除h的第三个元素\n") ;
ListDelete(h
,3,e
);
printf("(11)输出循环单链表h:");
DispList(h
) ;
printf("(12)释放循环单链表h\n") ;
DestroyList(h
) ;
return 1;
}
3.删除循环单链表中的奇数
已知循环单链表中结点值为整数, 要求编写算法删除此链表中的奇数。
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int data
;
struct ListNode
*next
;
};
struct ListNode
*createlist();
struct ListNode
*deleteeven( struct ListNode
*head
);
void printlist( struct ListNode
*head
)
{
struct ListNode
*p
= head
;
while (p
) {
printf("%d ", p
->data
);
p
= p
->next
;
}
printf("\n");
}
int main()
{
struct ListNode
*head
;
head
= createlist();
head
= deleteeven(head
);
printlist(head
);
return 0;
}
typedef struct ListNode
*List
;
struct ListNode
*createlist()
{
List head
=NULL, tail
=NULL;
int t
;
scanf("%d",&t
);
while(t
!= -1){
List temp
= (List
)malloc(sizeof(struct ListNode
));
temp
->data
= t
;
temp
->next
= NULL;
if(tail
==NULL)
head
= tail
= temp
;
else{
tail
->next
= temp
;
tail
= temp
;
}
scanf("%d",&t
);
}
return head
;
}
struct ListNode
*deleteeven( struct ListNode
*head
)
{
List p
=head
,q
;
while(head
&& ((head
->data
%2)-1)==0){
p
= head
;
head
= head
->next
;
free(p
);
}
p
= head
;
while(p
&& p
->next
)
{
while(p
->next
&&((p
->next
->data
%2)-1)==0)
{
q
= p
->next
;
p
->next
= q
->next
;
}
p
= p
->next
;
}
return head
;
}
4.双链表降序排列
编写算法将双链表中的结点按值降序排列。
#include<stdio.h>
#include<stdlib.h>
#define N 5
typedef struct node
{
int data
;
struct node
* next
;
}
ElemSN
;
ElemSN
* Createlink(int a
[],int n
)
{
int i
;
ElemSN
* h
=NULL, * p
;
for( i
=N
-1;i
>=0;i
--)
{
p
=(ElemSN
*)malloc(sizeof(ElemSN
));
p
->data
=a
[i
];
p
->next
=h
;
h
=p
;
}
return h
;
}
void printlink(ElemSN
* h
)
{
ElemSN
* p
;
for(p
=h
;p
;p
=p
->next
)
printf("%d\n",p
->data
);
}
ElemSN
* SelectSont(ElemSN
*h
)
{
ElemSN
*p
,*q
,*Pm
,*Qm
,*h1
;
h1
=NULL;
while(h
)
{
for(Pm
=q
=h
,p
=h
->next
;p
;q
=p
,p
=p
->next
)
{
if(Pm
->data
>p
->data
)
{
Pm
=p
;
Qm
=q
;
}
}
if(Pm
-h
)
Qm
->next
=Pm
->next
;
else
h
=h
->next
;
Pm
->next
=h1
;
h1
=Pm
;
}
return h1
;
}
int main(void)
{
int a
[N
]={11,3,81,9,5};
ElemSN
* head
;
head
=Createlink(a
,9);
head
=SelectSont(head
);
printlink(head
);
}