目录
目的∶
一个元素插入表中,不改变原有大小顺序。
实现思路∶元素x要大于等于前一个元素并且小于等于后一个元素。
然后就把这个位置之后的元素每一个都往后挪一位,把元素x插在这里
if (PL->data[i - 1] <= x && x <= PL->data[i])
{
for (j = PL->Length - 1; j >= i; j--)
{
PL->data[j + 1] = PL->data[j];
}
PL->data[j + 1] = x;
另一种情况是元素x比表中所有数都大,遍历完后,插在表尾。
然后需要注意的一点事,当插入的元素与表中元素相时会出现一
个问题,就是会把把相邻的三个数字都变成元素x。所以要及时跳出循环。
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 10
typedef struct Node
{
int data[MAXSIZE];//数据域
int Length;//实际存有数据的长度
}Seqlist, * PSeqlist;
/*顺序表创建*/
PSeqlist Create_Seqlist(int a[])
{
int i;
PSeqlist PL = (PSeqlist)malloc(sizeof(Seqlist));
if (!PL)
{
printf("申请空间失败");
}
else {
for (i = 1; i < 7; i++)
{
PL->data[i - 1] = a[i - 1];
}
PL->Length = i - 1;
}
return (PL);
}
/*顺序插入*/
void Sequence_Insert_Seqlist(PSeqlist PL, int x)
{
int i, j;
int count = 0;
for (i = PL->Length - 1; i >= 0; i--)
{
if (PL->data[i - 1] <= x && x <= PL->data[i])
{
for (j = PL->Length - 1; j >= i; j--)
{
PL->data[j + 1] = PL->data[j];
}
PL->data[j + 1] = x;
count++;
break;//及时跳出循环防止插入多个相同元素,可以插入与表中元素相同的元素
}
}
if (!count)//遍历到表尾依然没有合适的位置,说明比表中每一个元素都大,则插在表尾。
{
PL->data[6] = x;
}
}
void Destroy_SeqList(PSeqlist* PL)
{
if (*PL)
free(*PL);
*PL = NULL;
printf("顺序表已销毁\n");
return;
}
void Output_SeqList(PSeqlist PL)
{
int i;
for (i = 0; i <= PL->Length; i++)//因为要插入一个元素所以要多打一位。
{
printf("%3d", PL->data[i]);
//printf("%d", i);
}
}
int main()
{
int a[6] = { 1,2,4,5,6,7 };
PSeqlist PL = Create_Seqlist(a);
Sequence_Insert_Seqlist(PL, 3);
Output_SeqList(PL);
return 0;
}
2.单链表
思路总是和上面一样,主要是一些链表的操作相关的。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{
int data;
struct Node* next;
}Node, * Linklist;
/*创建链表*/
Linklist Create_Linklist(int n, int a[])
{
Linklist head = (Linklist)malloc(sizeof(Node));//头节点
if (head == NULL)
{
printf("申请空间失败");
}
else {
head->next = NULL;//头节点指针域置空,因为链表判空就是节点指针为空,所以头节点指针域置空更好判空
Linklist p = head;//头指针指向头节点
Linklist node;//节点指针
if (n > 0)
{
for (int i = 1; i <= n; i++)
{
node = (Linklist)malloc(sizeof(Node));
if (node)
{
node->data = a[i - 1];
p->next = node;
p = node;
}
else
{
printf("申请空间失败");
}
}
}
p->next = NULL;
}
return(head);
}
/*打印链表*/
void Print_Linklist(Linklist L)
{
Linklist head = L;
if (head == NULL || head->next == NULL)//头指针为空或头指针所指节点的下一个地址为空,即首元节点为空,
{
printf("链表为空");
}
else
{
while (head->next)
{
printf("链表元素为%d\n", head->next->data);
head = head->next;
}
printf("\n");
}
}
void Sequence_Insert_Linklist(Linklist PL, int x)
{
Linklist p = PL->next;
Linklist q = PL->next->next;
int count = 0;
while (q)
{
if (p->data <= x && x <= q->data)
{
Linklist node = (Linklist)malloc(sizeof(Node));
if (node)
{
node->data = x;
p->next = node;
node->next = q;
count++;
break;//及时跳出循环防止插入多个相同元素,可以插入与表中元素相同的元素。
}
}
p = p->next;
q = q->next;
}
if (!count)//遍历到表尾依然没有合适的位置,说明比表中每一个元素都大,则插在表尾。
{
Linklist node = (Linklist)malloc(sizeof(Node));
if (node)
{
p->next = node;
node->data = x;
node->next = NULL;
}
}
}
int main()
{
int a[6] = { 1,2,4,5,6,7 };
Linklist PL = Create_Linklist(6, a);
Sequence_Insert_Linklist(PL, 3);
Print_Linklist(PL);
return 0;
}
到这里结束了,感谢你的阅读(๑•̀㉨•́ฅ✧ 早