DS01-线性表
作者:互联网
1.本周内容总结
1.1总结线性表内容
- 顺序表结构体定义
typedef struct LNode *List
struct LNode
{
ElementType Data[MAXSIZE];
int last;
};
struct LNode L;
List PtrL;
- 顺序表插入
void Insert(ElementType X, int i; List PtrL)
{
int j;
if (PtrL->Last == MAXSIZE - 1)
{
cout << "表满"<<endl;
return;
}
if (i < 1 || i> PtrL->Last + 2)
{
cout << "位置不合法" << endl;
return;
}
for (j = PtrL->Last; j >= i - 1; j--)
{
PtrL->Data[j + 1] = PtrL->Data[j];
}
PtrL->Data[i - 1] = X;
PtrL->Last++;
return;
}
- 顺序表删除
void Delete(int i, List PtrL)
{
int j;
if (i<1 || i>PtrL->Last + 1)
{
cout << "不存在第" << i << "个元素" << endl;
return;
}
for (j = i; j <= PtrL->Last; j++)
{
PtrL->Data[j - 1] = PtrL->Data[j];
}
PtrL->Last--;
return;
}
- 链表结构体定义
typedef struct LNode *List
struct LNode
{
ElementType Data;
List next;
}
struct LNode L;
List PtrL;
- 链表头插法及尾插法
List Insert(ElementType X, int i, List PtrL)
{
List p, s;
if (i == 1)/*新结点插入在表头*/
{
s == new LNode;/*申请、填装结点*/
s->Data = x;
s->Next = PtrL;
return s;/*返回新表头指针*/
}
p = FindKth(i - 1, PtrL);/*查找第i-1个结点*/
if (p == NULL)/*第i-1个不存在,不能插入*/
{
cout << "参数i错" << endl;
return NULL;
}
else
{
s = new LNode;/*申请、填装结点*/
s->Data = X;
s->Next = p->Next;/*新结点插入在第i-1个结点的后面*/
p->Next = s;
return PtrL;
}
}
- 链表删除
List Delete(int i, List PtrL)
{
List p, s;
if (i == 1)/*若要删除的是第一个结点*/
{
s = PtrL;/*s指向第一个结点*/
if (PtrL != NULL)
{
PtrL = PtrL->Next;/*从链表中删除*/
}
else
{
return NULL;
}
delete s;/*释放被删除结点*/
return PtrL;
}
p = FindKth(i - 1, PtrL);/*查找第i-1个结点*/
if (p == NULL)
{
cout << "第" << i - 1 << "个结点不存在" << endl;
return NULL;
}
else if(p->Next==NULL)
{
cout << "第" << i << "个结点不存在" << endl;
return NULL;
}
else
{
s = p->Next;/*s指向第i个结点*/
p->Next = s->Next;/*从链表中删除*/
delete s;/*释放被删除结点*/
return PtrL;
}
标签:结点,线性表,int,List,PtrL,Next,DS01,Data 来源: https://www.cnblogs.com/ituqiewoe/p/12390636.html