文章目录
前言
总结一下最近学习数据结构的心得,并分享在学习中遇到的困难
实验一:链表的相关操作、算法和应用
1.合并两个递增有序的单循环链表
代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef int DataType;
struct Node
{
DataType data;
struct Node * next;
};
typedef struct Node Node;
typedef struct Node *PNode;
typedef struct Node *LinkList;
PNode createEmptyLinkedList()
{
PNode current;
current = (PNode)malloc(sizeof(Node));
current->next = NULL;
current->data = -1;
return current;
}
PNode buildCircularLinkedList(int n, PNode tail)
{
PNode current=NULL, prev;
prev = tail;
for (int i = 0; i < n; i++)
{
current = (PNode)malloc(sizeof(Node));
current->next = NULL;
scanf("%d", ¤t->data);
prev->next = current;
prev = current;
}
current->next = tail->next;
tail->next = current;
return tail;
}
PNode mergeNDeduplicateList(PNode tail1, PNode tail2)
{
PNode LinkNode;
LinkNode = tail2->next->next;
tail2->next->next = tail1->next->next;
tail1->next->next = LinkNode;
free(tail2);
PNode StopNode,pre,p;
StopNode = tail1->next->next;
while(StopNode != tail1->next)
{
pre = StopNode;
p = pre->next;
while(pre != tail1->next)
{
if(p->data == StopNode->data)
{
if(p == tail1->next)
tail1->next = pre;
PNode temp;
temp = p;
pre->next = p->next;
p = pre->next;
free(temp);
}
else
{
pre = pre->next;
p = pre->next;
}
}
StopNode = StopNode->next;
}
do
{
pre = tail1->next->next;
p = pre->next;
while(pre != tail1->next)
{
if(p->data < pre->data)
{
DataType TempData;
TempData = p->data;
p->data = pre->data;
pre->data = TempData;
}
else
{
pre = pre->next;
p = pre->next;
}
}
StopNode = StopNode->next;
}
while(StopNode != tail1->next);
return tail1;
}
void printCircularLinkedList(PNode tail)
{
PNode current, last;
last = tail->next;
current = last->next;
do
{
printf("%d ", current->data);
current = current->next;
} while (current != last->next);
}
int main()
{
PNode list1, list2;
int list1_number, list2_number;
list1 = createEmptyLinkedList();
list2 = createEmptyLinkedList();
scanf("%d", &list1_number);
buildCircularLinkedList(list1_number, list1);
scanf("%d", &list2_number);
buildCircularLinkedList(list2_number, list2);
list1 = mergeNDeduplicateList(list1, list2);
printCircularLinkedList(list1);
return 0;
}
该程序的主要思想在于:
1、将两个链表链接;
2、查重,将重复的数据删除;
3、将数据进行排序;
2.链表中奇偶结点的移动
代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef int DataType;
struct Node
{
DataType data;
struct Node* next;
};
typedef struct Node *PNode;
typedef struct Node *LinkList;
LinkList CreateList_Tail_loop()
{
LinkList head = (LinkList)malloc(sizeof(struct Node));
PNode cur = NULL;
PNode tail = head;
DataType data;
scanf_s("%d", &data);
while (data != -1)
{
cur = (struct Node*)malloc(sizeof(struct Node));
cur->data = data;
tail->next = cur;
tail = cur;
scanf_s("%d", &data);
}
tail->next = head;
return tail;
}
PNode Move_Odd_Even(LinkList tail)
{
PNode pre = tail->next;
PNode head = tail->next;
PNode P2 = NULL;
PNode p = pre->next;
PNode temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = 0;
tail->next = temp;
temp->next = head;
tail = temp;
while(p->data)
{
if(p->data%2 ==0)
{
P2 = p;
p = p->next;
pre->next = p;
tail->next = P2;
tail = P2;
}
else
{
p = p->next;
pre = pre->next;}
}
pre->next = temp->next;
tail->next = head;
free(temp);
return tail;
}
void print(LinkList tail)
{
PNode head = tail->next;
PNode p = head->next;
while (p != head)
{
printf("%d ", p->data);
p = p->next;
}
}
void DestoryList_Link(LinkList tail)
{
PNode pre = tail->next;
PNode p = pre->next;
while (p != tail)
{
free(pre);
pre = p;
p = pre->next;
}
free(pre);
free(tail);
}
int main()
{
LinkList tail = NULL;
LinkList p = NULL;
tail = CreateList_Tail_loop();
p = Move_Odd_Even(tail);
print(p);
DestoryList_Link(tail);
return 0;
}
该程序的主要思想在于:
1、遍历栈,找到偶数
2、利用尾插法将偶数插入队尾
3.多项式的加法
#include<stdio.h>
#include<stdlib.h>
typedef int DataType;
struct Node
{
DataType dataX,dataY;
struct Node* next;
};
typedef struct Node *PNode;
typedef struct Node *LinkList;
LinkList SetNullList_Link()
{
LinkList head = (LinkList)malloc(sizeof(struct Node));
if (head != NULL)
head->next = NULL;
else
printf("alloc failure");
return head;
}
void CreateList(struct Node* head)
{
PNode p = NULL;
PNode q = head;
int dataX,dataY;
scanf("%d,%d", &dataX,&dataY);
while (dataX != 0 || dataY !&#