
for(j=L->last j>=i-1 j--)
L->data[j+1] = L->data[j]
L->data[i-1] = x
// 更新顺序表长度
L->last++
楼主你好具体代码如下:
#include <stdio.h>
int insert(int a[], int n, int pos, int value)//插入函数
{
int i
if(pos>n)
return 0
if(n>100)
return -1
for(i=ni>=posi--)
a[i]=a[i-1]
a[pos-1]=value
return 1
}
int main(void)
{
int i, n, pos, value, flag
int a[100]
printf("Input: \n")
scanf("%d", &n) //初始元素个数
for (i = 0i <ni++)
{
scanf("%d", &a[i])
}
scanf("%d%d", &pos, &value) //需要插入元素的位置和值
flag = insert(a, n, pos, value)
printf("Output: ")
if (flag == 0)
{
printf("position is wrong!\n")
}
else if (flag == -1)
{
printf("table is already full!\n")
}
else
{
for (i = 0i <= ni++)
{
printf("%d ", a[i])
}
printf("\n")
}
return 0
}
楼上的是错误的
经过编译测试 结果完全符合楼主的要求
Input:
5
1 2 3 4 5
3 12
ouput:1 2 12 3 4 5
input:
5
1 2 3 4 5
7 10
output:position is wrong
希望能帮助你哈^_^
向一个空顺序线性表中存入一系列数据元素#include <stdio.h>
#include <stdlib.h>
/*
1.创建结构体-----具体事物的抽象
2.创建链表
3.创建结点
4.插入 *** 作
4.1 表头插入
4.2 表尾插入
4.3 指定位置插入(指定位置的前面)
5.删除 *** 作
5.1 表头删除
5.2 表尾删除
5.3 指定位置删除
6.判断是否为空
7.打印链表
*/
//单链表的结构体
typedef struct SingleList
{
//数据域
int data //以这个数据为例
//struct MM myMM
//指针域
struct SingleList *next
}LIST,*LPLIST
/*
别名:习惯大写
起别名---小名
//好处:单词少,好看(含义更精简)
struct SingleList 换一种叫法: LIST
strcut SingleList * 换一种叫法: LPLIST
*/
//------->2.创建链表 ---任何结构都需要用一个东西去表示
LPLIST CreateList()
{
//创建过程就是初始化过程---初始化基本数据成员过程
//需要内存空间
LPLIST List = (LPLIST)malloc(sizeof(LIST))
if (List == nullptr)
{
printf("失败了\n")
system("pause")
exit(0)
}
//初始化基本数据成员----有表头的链表
List->next = nullptr
return List
}
//------->3.创建结点
LPLIST CreateNode(int data)
{
//1.需要内存
LPLIST Node = (LPLIST)malloc(sizeof(LIST))
//2.初始化基本数据成员
Node->data = data //和创建链表多了数据域
Node->next = nullptr
return Node
}
//------->4.1 头插法
//函数写法:形参可以表示要 *** 作的东西
//插入的链表是(List),插入的数据是多少(data)
void InsertListHeadNode(LPLIST List,int data)
{
//插入:创建插入的结点
LPLIST newNode = CreateNode(data) //创建结点
//插入 *** 作
newNode->next = List->next
/*
c=1
b=2
a=c
c=b
*/
List->next = newNode
}
//------->4.2 尾插法
void InsertListTailNode(LPLIST List, int data)
{
//找到表尾--->定义一个移动的指针
LPLIST tailNode = List
while (tailNode->next != nullptr)
{
tailNode = tailNode->next
}
//创建插入的结点
LPLIST newNode = CreateNode(data)
tailNode->next = newNode
}
//------->4.3 指定位置
void InsertListAppoinNode(LPLIST List, int data, int PosData)
{
//创建两个移动的指针:去找指定位置和指定位置的前面
LPLIST frontNode = List
//frontNode->next==taiNode:判断相邻
LPLIST tailNode = List->next
//判断是否为空
while (tailNode->data != PosData)
{
/*
frontNode=frontNode->next
tailNode=tailNode->next
*/
frontNode = tailNode
tailNode = frontNode->next
if (tailNode == nullptr)
{
printf("未找到指定位置\n")
system("pause")
exit(0)
}
}
//tailNode->data=data
//找到后创建插入的结点
LPLIST newNode = CreateNode(data)
frontNode->next = newNode
newNode->next = tailNode
}
//------->5.判断是否为空
//和创建的时候比较
int IsEmptyList(LPLIST List)
{
if (List->next == nullptr)
return 1 //返回1表示为空
return 0 //表示不为空
}
////------->6.打印数据
void PrintList(LPLIST List)
{
if (IsEmptyList(List))
{
printf("链表为空,无法打印")
system("pause")
exit(0)
}
LPLIST pNext = List->next
while (pNext != nullptr)
{
printf("%d\t", pNext->data)
pNext = pNext->next
}
printf("\n")
}
//------->7.删除
//头删除
void DeleteListHeadNode(LPLIST List)
{
if (IsEmptyList(List))
{
printf("链表为空,无法删除\n")
system("pause")
exit(0)
}
LPLIST DNode = List->next
List->next = DNode->next
free(DNode)
DNode = nullptr
}
//------->8.尾删
void DeleteListTailNode(LPLIST List)
{
if (IsEmptyList(List))
{
printf("链表为空,无法删除\n")
system("pause")
exit(0)
}
//找到表尾--->定义一个移动的指针
LPLIST tailNode = List->next
LPLIST tailFront = List
while (tailNode->next != nullptr)
{
tailFront = tailNode
tailNode = tailFront->next
}
tailFront->next = nullptr
free(tailNode)
}
//------->9.指定位置删除
void DeleteListAppoinNode(LPLIST List, int PosData)
{
//创建两个移动的指针:去找指定位置和指定位置的前面
LPLIST frontNode = List
//frontNode->next==taiNode:判断相邻
LPLIST tailNode = List->next
//判断是否为空
while (tailNode->data != PosData)
{
/*
frontNode=frontNode->next
tailNode=tailNode->next
*/
frontNode = tailNode
tailNode = frontNode->next
if (tailNode == nullptr)
{
printf("未找到指定位置\n")
system("pause")
exit(0)
}
}
frontNode->next = tailNode->next
free(tailNode)
}
int main()
{
LPLIST List = CreateList() //List创建成功
printf("插入:\n")
InsertListHeadNode(List, 1)
InsertListHeadNode(List, 2)
InsertListHeadNode(List, 3)
InsertListTailNode(List, 0)
PrintList(List)
printf("删除:\n")
DeleteListHeadNode(List)
PrintList(List)
DeleteListTailNode(List)
PrintList(List)
printf("指定位置:\n")
//看不懂,可以找群主
InsertListAppoinNode(List, 4, 2)
InsertListAppoinNode(List, 3, 2)
//C/C++ 8群
PrintList(List)
// 491994603
DeleteListAppoinNode(List, 2)
PrintList(List)
system("pause")
return 0
}
向一个空顺序线性表中存入一系列数据元素,我使用了多种插入方式,而对你来说只需要其中一种方式,通过循环的方式去插入就好了.
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)