C语言顺序存储链表如何实现插入一个元素删除一个元素,清写一个简单的,谢谢!!!

2025-03-05 05:09:49
推荐回答(1个)
回答1:

#include "stdafx.h"
#include 
using namespace std;

typedef struct
{
int *elem;
int length;
int size;
}SqList;

void InitList(SqList &L)
{
L.elem=new int [100];
L.length=0;
L.size=100;
}
void ListInsert(SqList &L,int pos,int e)
{
if(pos>0&&pos<=L.length)
{
int i;
for(i=L.length;i>pos-1;i--)
{
L.elem[i]=L.elem[i-1];
}
L.elem[i]=e;
L.length++;
}
else cout<<"插入的位置不合法!"<}

void ListDelete(SqList &L,int pos,int &e)
{
if(pos<1||pos>L.length)cout<<"删除的位置不合法!"< else
{
int i;
e=L.elem[pos-1];
for(i=pos-1;i {
L.elem[i]=L.elem[i+1];
}
L.length--;
}
}
int main()
{
SqList L;
InitList(L);int e;
int a[]={1,2,3,4,5};
CreateList(L,a,5);
ListInsert(L,5,6);
ListDelete(L,6,e);
TravelList(L);
system("pause");
return 0;
}