#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Elemtype;
typedef struct LinkListNode {
Elemtype date;
struct LinkListNode* next;
} LinkListNode_t, *LinkList_p;
LinkList_p CreatEmptyList(void)
{
LinkList_p newlist = (LinkList_p)malloc(sizeof(LinkListNode_t));
if (newlist == NULL)
return NULL;
newlist->next = NULL;
return newlist;
}
void ElemFuc(Elemtype* dst, const Elemtype* src)
{
*dst = *src;
}
LinkList_p CreatList(Elemtype* date, int num, void CopyFuc(Elemtype*, const Elemtype*))
{
LinkList_p newlist = (LinkList_p)malloc(sizeof(LinkListNode_t));
if (newlist == NULL)
return NULL;
newlist->next = NULL;
LinkList_p temp = newlist;
LinkList_p newnode;
int i = 0;
while (num--) {
newnode = (LinkList_p)malloc(sizeof(LinkList_p));
if (newnode == NULL)
return newlist;
newnode->next = NULL;
CopyFuc(&newnode->date, &date[i++]);
temp->next = newnode;
temp = newnode;
}
return newlist;
}
int DelList(LinkList_p l)
{
if (l == NULL)
return 1;
if (l->next == NULL) {
free(l);
return 2;
}
LinkList_p temp;
while (l) {
temp = l->next;
free(l);
l = temp;
}
return 3;
}
typedef enum {
Previous = 0,
Self,
} Location_t;
bool InterageEqual(const Elemtype* a, const Elemtype* b)
{
return *a == *b ? true : false;
}
void InsertANode(LinkList_p la, LinkList_p lb)
{
LinkList_p temp = la->next;
lb->next = temp;
la->next = lb;
}
void InsertNodefromOneList(LinkList_p dst, LinkList_p psrc, LinkList_p src)
{
psrc->next = src->next;
InsertANode(dst, src);
}
int MoveListNode(LinkList_p Dstl, LinkList_p PreDstl, LinkList_p Srcl)
{
if (Dstl == NULL || Srcl == NULL)
return 0;
LinkList_p temp = Dstl->next;
Dstl->next = Srcl->next;
Srcl->next = Dstl;
PreDstl->next = temp;
return 1;
}
int reversal(LinkList_p l)
{
if (l == NULL || l->next == NULL || l->next->next == NULL)
return 1;
LinkList_p temp = l->next;
LinkList_p atemp = l->next;
for (; atemp->next != NULL; temp = atemp) {
MoveListNode(temp->next, temp, l);
}
return 1;
}
bool InterageBig(const Elemtype* a, const Elemtype* b)
{
return *a > *b ? true : false;
}
LinkList_p GetValueLocation(LinkList_p l, const Elemtype date, bool EqualFuc(const Elemtype*, const Elemtype*), Location_t loction)
{
if (l == NULL || l->next == NULL)
return NULL;
LinkList_p temp = l;
while (temp->next != NULL && !EqualFuc(&temp->next->date, &date)) {
temp = temp->next;
}
if (temp->next != NULL) {
return loction == Self ? temp->next : temp;
}
return NULL;
}
int ListLength(LinkList_p l);
LinkList_p mergeList(LinkList_p la, LinkList_p lb)
{
if (la == NULL || lb == NULL)
return NULL;
if (la->next == NULL && lb->next == NULL) {
free(lb);
return la;
}
if (la->next == NULL) {
free(la);
return lb;
}
if (lb->next == NULL) {
free(lb);
return la;
}
LinkList_p temp_dst = la;
while (lb->next) {
temp_dst = GetValueLocation(temp_dst, lb->next->date, InterageBig, Previous);
InsertNodefromOneList(temp_dst, lb, lb->next);
}
free(lb);
return la;
}
int FrontInsertValueList(LinkList_p l, const Elemtype gdate, const Elemtype adate, void CopyFuc(Elemtype*, const Elemtype*))
{
LinkList_p temp = GetValueLocation(l, gdate, InterageEqual, Previous);
if (temp == NULL)
return 1;
LinkList_p newnode = (LinkList_p)malloc(sizeof(LinkList_p));
if (newnode == NULL)
return 2;
CopyFuc(&newnode->date, &adate);
newnode->next = temp->next;
temp->next = newnode;
return 3;
}
int BehindInsertValueList(LinkList_p l, const Elemtype gdate, const Elemtype adate, void CopyFuc(Elemtype*, const Elemtype*))
{
LinkList_p temp = GetValueLocation(l, gdate, InterageEqual, Self);
if (temp == NULL)
return 1;
LinkList_p newnode = (LinkList_p)malloc(sizeof(LinkList_p));
if (newnode == NULL)
return 2;
CopyFuc(&newnode->date, &adate);
newnode->next = temp->next;
temp->next = newnode;
return 3;
}
LinkList_p GetIndexLocation(LinkList_p l, Elemtype index)
{
if (l == NULL)
return NULL;
LinkList_p temp = l;
while (--index && temp) {
temp = temp->next;
}
if (index)
return NULL;
if (temp == NULL)
return NULL;
return temp;
}
LinkList_p GetMaxArraySum(LinkList_p l, int num, Elemtype* sum)
{
if (l == NULL || l->next == NULL)
return NULL;
if (l->next->next == NULL && num == 1) {
*sum = l->next->date;
return l->next;
}
*sum = 0;
int sum_date = 0;
LinkList_p maxhead = NULL;
LinkList_p temp1;
LinkList_p temp2;
LinkList_p temp3;
for (temp1 = l->next, temp2 = GetIndexLocation(temp1, num); temp1 && temp2; temp1 = temp3->next, temp2 = GetIndexLocation(temp3->next, num)) {
sum_date = 0;
temp3 = temp1;
while (temp1 != temp2) {
sum_date += temp1->date;
temp1 = temp1->next;
}
if (temp1 == temp2) {
sum_date += temp1->date;
*sum = *sum > sum_date ? *sum : sum_date;
maxhead = *sum > sum_date ? maxhead : temp3;
}
}
return maxhead;
}
int InsertIndexList(LinkList_p l, int index, const Elemtype adate, void CopyFuc(Elemtype*, const Elemtype*))
{
LinkList_p temp = GetIndexLocation(l, index);
if (temp == NULL)
return 1;
LinkList_p newnode = (LinkList_p)malloc(sizeof(LinkList_p));
if (newnode == NULL)
return 2;
CopyFuc(&newnode->date, &adate);
newnode->next = temp->next;
temp->next = newnode;
return 3;
}
#define ERROR_ELEM INT_MAX
Elemtype DelNode(LinkListNode_t* l)
{
int i = 0;
if (l == NULL || l->next == NULL)
return ERROR_ELEM;
LinkList_p temp = l->next->next;
i = l->next->date;
free(l->next);
l->next = temp;
return i;
}
int DelOneIndexList(LinkList_p l, int index, Elemtype DelNode(LinkListNode_t*))
{
LinkList_p temp = GetIndexLocation(l, index);
Elemtype date = DelNode(temp);
return date;
}
Elemtype* DelIndexList(LinkList_p l, int index, int num, int* length, Elemtype DelNode(LinkListNode_t*))
{
LinkList_p temp = GetIndexLocation(l, index);
Elemtype* date = malloc(sizeof(Elemtype) * num);
int i;
for (i = 0; i < num; i++) {
date[i] = DelNode(temp);
if (date[i] == ERROR_ELEM)
break;
}
*length = i;
return date;
}
Elemtype DelOneValueList(LinkList_p l, Elemtype value, Elemtype DelNode(LinkListNode_t*))
{
LinkList_p temp = GetValueLocation(l, value, InterageEqual, Previous);
Elemtype date = DelNode(temp);
return date;
}
int ListLength(LinkList_p l);
Elemtype* DelValueList(LinkList_p l, Elemtype value, int* length, Elemtype DelNode(LinkListNode_t*))
{
LinkList_p temp = GetValueLocation(l, value, InterageEqual, Previous);
Elemtype* date = malloc(sizeof(Elemtype) * ListLength(l));
int i = 0;
while (temp) {
date[i++] = DelNode(temp);
temp = GetValueLocation(temp, value, InterageEqual, Previous);
}
*length = i;
return date;
}
int ListLength(LinkList_p l)
{
int i = 0;
LinkList_p temp = l->next;
while (temp) {
i++;
temp = temp->next;
}
return i;
}
void visit(Elemtype* date)
{
if (date == NULL)
return;
printf("%d", *date);
}
void List_Info(LinkList_p l, void visit(Elemtype*))
{
printf("------------------\n");
if (l == NULL) {
printf("Error! the List is nonexistent.\n");
return;
}
if (ListLength(l) == 0) {
printf("The list is Empty.\n");
return;
}
LinkList_p temp = l->next;
int i = 0;
while (temp) {
visit(&temp->date);
temp = temp->next;
printf(" ");
if (i++ + 1 % 10 == 0)
printf("\n");
}
printf("\n");
}
int main(void)
{
Elemtype a[10] = { 19, 9, 9, 2, 40, 4, 7, 8, 9, 10 };
Elemtype la_date[4] = { 2, 4, 6 ,11};
Elemtype lb_date[4] = { 1, 3, 5 ,10};
LinkList_p l = CreatList(a, 10, ElemFuc);
LinkList_p la = CreatList(la_date, 4, ElemFuc);
LinkList_p lb = CreatList(lb_date, 4, ElemFuc);
List_Info(l, visit);
List_Info(la, visit);
List_Info(lb, visit);
mergeList(la,lb);
List_Info(la, visit);
// InsertNodefromOneList(la, lb, lb->next);
// List_Info(la, visit);
// List_Info(lb, visit);
// LinkList_p temp_dst = GetValueLocation(la, lb->next->date, InterageBig, Previous);
// InsertNodefromOneList(temp_dst, lb, lb->next);
// List_Info(la, visit);
// List_Info(lb, visit);
// // LinkList_p mergelist = mergeList(la,lb);
// List_Info(mergelist, visit);
DelList(l);
DelList(la);
// DelList(lb);
return 0;
}
链表的实现
最新推荐文章于 2024-08-22 23:45:07 发布