注:链表的结构应该为:
指针头(可省略)->结构头->数据块->链表尾(NULL)
1、单向链表应包含数据域与指向下一个结点的指针域两部分
2、在插入新结点时,只需将自己的next设为上一个结点的next,上一个结点的next设为自己即可完成.
node->next = lastNode->next;
lastNode->next = node;
3、删除当前结点时,只需将上一个结点的next设为自己的next,并free自己
lastNode->next=node->next;
free(node);
具体链表实现代码(题引申,不包含插入删除)
//(1)将两个递增的有序链表合并为一个递增的有序链表。要求结果链表仍使用原来两个链表的存储空间, 不
//(7)设计一个算法,通过遍历一趟,将链表中所有结点的链接方向逆转,仍利用原表的存储空间。
另外占用其它的存储空间。表中不允许有重复的数据。
#include "stdio.h"
#include "pch.h"
typedef struct chainList {
int data;
struct chainList *next;
}Node;
struct chainList * createChainList(int n, int length) {
int i;
struct chainList * headNode = (struct chainList *)malloc(sizeof(struct chainList));;
struct chainList * dataNode = headNode;
headNode->data = n;
for (i = 0; i < length; i++) {
struct chainList *node = (struct chainList *)malloc(sizeof(struct chainList));
dataNode->next = node;
dataNode = node;
}//创建空间
dataNode = headNode;
for (i = 0; i < n; i++) {
struct chainList *node = dataNode->next;
printf("输入第%d位数据:", i + 1);
scanf_s("%d", &node->data);
dataNode->next = node;
dataNode = node;
}//具体输入值
dataNode->next = NULL;
return headNode;
}
struct chainList * sortChainList(struct chainList *listA, struct chainList *listB) {
struct chainList * aDataNode = listA->next;
struct chainList * bDataNode = listB->next;
struct chainList * listC = (struct chainList *)malloc(sizeof(struct chainList));;
struct chainList * cDataNode = listC;
int i = 0;
listC->data = aDataNode->data + bDataNode->data;
while (aDataNode && bDataNode) {
if (aDataNode->data < bDataNode->data) {
cDataNode->next = aDataNode;
cDataNode = aDataNode;
aDataNode = aDataNode->next;
}
else {
cDataNode->next = bDataNode;
cDataNode = bDataNode;
bDataNode = bDataNode->next;
}
if (aDataNode && aDataNode->data == cDataNode->data) {
aDataNode = aDataNode->next;
listC->data--;
}
if (bDataNode && bDataNode->data == cDataNode->data) {
bDataNode = bDataNode->next;
listC->data--;
}
}
if (aDataNode) {
cDataNode->next = aDataNode;
}
else {
cDataNode->next = bDataNode;
}
return listC;
}
void ListPrint(struct chainList *headNode) {
struct chainList * dataNode = headNode->next;
while (dataNode) {
printf("%d ",dataNode->data);
dataNode = dataNode->next;
}
printf("\n");
}
void reserveChainList(struct chainList *headNode) {
struct chainList * nowNode = headNode->next;
struct chainList * currentNode = NULL;
struct chainList * nextNode = NULL;
struct chainList * nextNextNode= nowNode->next;
while (nextNextNode->next) {
currentNode = nowNode;
nextNode = nextNextNode;
nextNextNode = nextNode->next;
nextNode->next = currentNode;
if (currentNode->data == headNode->next->data) {
currentNode->next = NULL;
}
nowNode = nextNode;
}
nextNextNode->next = nowNode;
headNode->next = nextNextNode;
}
int main(int argc, char** argv) {
int n = 0;
int length = 20;
printf("输入链表的长度(请不要超过10):");
scanf_s("%d", &n);
struct chainList * listA = createChainList(n, length);
printf("A链表内容为:");
ListPrint(listA);
struct chainList * listB = createChainList(n, length);
printf("B链表内容为:");
ListPrint(listB);
struct chainList * listC = sortChainList(listA, listB);
printf("C链表内容为:");
ListPrint(listC);
reserveChainList(listC);
printf("转置后C链表内容为:");
ListPrint(listC);
return 0;
}
插入与删除的实现:
void ListDeleteElement(struct chainList *listHeader,int i){
int j;
struct chainList * tempNode = listHeader;
struct chainList * deleteNode;
for(j = 0;j<i-1;j++){
tempNode = tempNode->next;
}
deleteNode = tempNode->next;
tempNode->next = deleteNode->next;
free(deleteNode);
listHeader->data--;
}
void ListInsertElement(struct chainList *listHeader,int i,int item){
int j;
struct chainList *tempNode = listHeader;
struct chainList *insertNode = (struct chainList *)malloc(sizeof(struct chainList));
insertNode->data = item;
for(j = 0;j<i-1;j++){
tempNode = tempNode->next;
}
insertNode->next = tempNode->next;
tempNode->next = insertNode;