Seqlist.cpp
#include"Sqlist.h"
#include<stdio.h>
#include<stdlib.h>
/* 链表初始化*/
void init(l** head)
{
*head = (l*)malloc(sizeof(l));
(*head)->next = NULL;
}
/*插入操作*/
int Insert(SLNode* head, int i, Shu x)
{
SLNode* p, * q;
int j = -1;
p = head;
while (p->next != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (j != i - 1)
{
printf("输入位置错误");
return 0;
}
q = (SLNode*)malloc(sizeof(SLNode));
q->data = x;
q->next = p->next;
p->next = q;
return 1;
}
/*删除节点值相同的节点*/
int deete(SLNode* head)
{
SLNode* ptr;//固定一端不动
SLNode* pts;//指向比较数据大小的节点位置
SLNode* pth;//指向pts的前一个位置
ptr = head;
pth = head;
pts = ptr->next;
while (ptr != NULL)
{
pts = ptr->next;
while (pts != NULL)
{
if (ptr->data == pts->data)
{
pth->next = pts->next;
free(pts);
pts = pth->next;
}
else
{
pth = pts;
pts = pts->next;
}
}
ptr = ptr->next;
}
return 1;
}
/*单链表打印*/
void printLIst(SLNode* head)
{
while (head->next != NULL)
{
head = head->next;
printf("%d ", head->data);
}
}
/*单链表逆置*/
int inverst(SLNode* head)
{
SLNode* ptr;//链表最前面的一个指针
SLNode* pts;//链表中间的一个指针
SLNode* pth;//链表最左边的一个指针
pts = head->next;
ptr = pts->next;
pts->next = NULL;
pth = NULL;
while (ptr != NULL)
{
pth = pts;
pts = ptr;
ptr = ptr->next;
pts->next = pth;
}
pts->next = pth;
head->next = pts;
return 1;
}
/*最小值节点*/
void min(SLNode* head)
{
int temp;
SLNode* ptr;
SLNode* pts;
SLNode* pth;
ptr = head->next;
pts = NULL;
pth = NULL;
while (ptr!=NULL)
{
pts = ptr->next;
while (pts!= NULL)
{
if (ptr->data >pts->data)
{
break;
}
pts = pts->next;
}
if (pts == NULL || ptr->next == NULL)
{
printf("%d ", ptr->data);
if (ptr->next != NULL)//判断是否有后继节点
{
if (ptr->data % 2 != 0)
{
temp = ptr->data;
ptr->data = ptr->next->data;
ptr->next->data = temp;
}
else
{
pth = ptr->next;
ptr->next = pth->next;
free(pth);
}
}
break;
}
else
{
ptr = ptr->next;
}
}
}
Sqlist.h
typedef int Shu;
/*定义结构体*/
typedef struct Node
{
Shu data;
struct Node* next;
}SLNode, l;
/* 链表初始化*/
void init(l** head);
/*插入操作*/
int Insert(SLNode* head, int i, Shu x);
/*删除多余节点*/
int deete(SLNode* head);
/*打印链表元素*/
void printLIst(SLNode* head);
/*单链表逆置*/
int inverst(SLNode* head);
/*最小值节点*/
void min(SLNode* head);
main()
#include<stdio.h>
#include<stdlib.h>
#include"Sqlist.h"
int main()
{
SLNode* head;
init(&head);
int n, m, count, k;
scanf_s("%d", &n);
for (int i = 0; i < n; i++)
{
scanf_s("%d", &m);
count = Insert(head, i, m);
}
min(head);
printLIst(head);
return 0;
}
实现功能的结构图如下:
其中一个运行代码的截图: