数据结构的删除操作


1.顺序表的删除:
bool ListDelete(SqList *&L,int i,ElemType &e) //删除数据元素
{
int j;
if (i < 1 || i > L -> length)//参数错误时返回false
return false;
i--; //将顺序表逻辑序号转化为物理序号
e = L -> data[i];
for (j = i;j < L -> length - 1;j++)//将data[i]之后的元素前移一个位置
L -> data[j] = L -> data[j + 1];
L -> length--;//顺序表长度减1
return true; //成功删除返回true
}



2.单链表的删除:
bool ListDelete(LinkList *&L,int i,ElemType &e) //删除数据元素
{
int j = 0;
LinkList *p = L, *q;//p指向头节点,j置为0(即头节点的序号为0)
while (j < i - 1 && p != NULL)//查找第i-1个节点
{ 
j++;
p = p -> next;
}
if (p == NULL)//未找到第i-1个节点,返回false
return false;
else //找到第i-1个节点*p
{ 
 q = p -> next;//q指向第i个节点
if (q == NULL)//若不存在第i个节点,返回false
return false;
e = q -> data;
p -> next = q -> next;//从单链表中删除*q节点
free(q); //释放*q节点
return true; //返回true表示成功删除第i个节点
}
}


3.双链表的删除:
bool ListDelete(DLinkList *&L, int i, ElemType &e) //删除数据元素
{
int j = 0;
DLinkList *p = L,*q;
while (j < i - 1 && p != NULL)
{
j++;
p = p -> next;
}
if (p == NULL)//未找到第i-1个结点
return false;
else //找到第i-1个结点*p
{
q = p -> next;//q指向要删除的结点
if (q == NULL) return false;//不存在第i个结点
e = q -> data;
p -> next = q -> next;//从单链表中删除*q结点
if (p -> next != NULL) p -> next -> prior = p;
free(q); //释放*q结点
return true;
}
}


4.循环单链表的删除:
bool ListDelete(LinkList *&L,int i,ElemType &e) //删除元素
{
int j = 0;
LinkList *p = L, *q;
if (p -> next != L)//原单链表不为空表时
{
if (i == 1) //i==1时
{
q = L -> next;//删除第1个结点
e = q -> data;
L -> next = q -> next;
free(q);
return true;
}
else //i不为1时
{
p = L -> next;
while (j < i - 2 && p != L)
{
j++;
p = p -> next;
}
if (p == L) //未找到第i-1个结点
return false;
else //找到第i-1个结点*p
{
q = p -> next;//q指向要删除的结点
e = q -> data;
p -> next = q -> next;//从单链表中删除*q结点
free(q); //释放*q结点
return true;
}
}
}
else //空表返回false
return false;
}


5.循环双链表的删除:
bool ListDelete(DLinkList *&L,int i,ElemType &e) //删除数据元素
{
int j = 0;
DLinkList *p = L,*q;
if (p -> next != L)//原双链表不为空表时
{ 
if (i == 1) //i==1时
{ 
q = L -> next;//删除第1个结点
e = q -> data;
L -> next = q -> next;
q -> next -> prior = L;
free(q);
return true;
}
else //i不为1时
{ 
p = L -> next;
while (j < i - 2 && p != NULL)
{
j++;
p = p -> next;
}
if (p == NULL)//未找到第i-1个结点
return false;
else //找到第i-1个结点*p
{
q = p -> next;//q指向要删除的结点
if (q == NULL) 
return false; //不存在第i个结点
e = q -> data;
p -> next = q -> next;//从单链表中删除*q结点
if (p -> next != NULL) p -> next -> prior = p;
free(q); //释放*q结点
return true;
}
}
}
else //原双链表为空表时
return false;
}


6.顺序栈的出栈:
bool Pop(SqStack *&s,ElemType &e) //出栈
{ 
if (s -> top == -1) //栈为空的情况,即栈下溢出
return false;
e = s -> data[s -> top];//取栈顶指针元素的元素
s -> top--; //栈顶指针减1
return true;
} 


7.链栈的出栈:
bool Pop(LiStack *&s,ElemType &e) //出栈
{ 
LiStack *p;
if (s -> next == NULL)//栈空的情况
return false;
p = s -> next;//p指向开始节点
e = p -> data;
s -> next = p-> next;//删除*p节点
free(p); //释放*p节点
return true;
}


8.顺序队列的出队:
bool deQueue(SqQueue *&q, ElemType &e) //出队
{
if (q -> front == q -> rear)//队空下溢出
return false;
q -> front = (q -> front + 1) % MaxSize;
e = q -> data[q -> front];
return true;
}


9.链队列的出队:
bool deQueue(LiQueue *&q, ElemType &e) //出队
{ QNode *t;
if (q -> rear == NULL)//队列为空
return false;
t=q->front; //t指向第一个数据节点
if (q -> front == q -> rear)  //队列中只有一个节点时
q -> front = q -> rear = NULL;
else //队列中有多个节点时
q -> front = q -> front -> next;
e = t -> data;
free(t);
return true;
}

10.顺序串的删除:

LiString *DelStr(LiString *s, int i, int j)
{ 
int k;
LiString *str, *p=s->next, *q, *r;
str = (LiString *)malloc(sizeof(LiString));
str -> next = NULL;
r = str; //r指向新建链表的尾节点
if (i <= 0 || i > StrLength(s) || j < 0 || i + j - 1 > StrLength(s))
return str; //参数不正确时返回空串
for (k = 0; k < i - 1; k++) //将s的前i-1个节点复制到str
{ 
q = (LiString *)malloc(sizeof(LiString));
q -> data = p -> data;
r -> next = q; r = q;
p = p -> next;
}
for (k = 0; k < j; k++)//让p沿next跳j个节点
p = p -> next;
while (p != NULL)//将*p及其后的节点复制到str
{ 
q = (LiString *)malloc(sizeof(LiString));
q -> data = p -> data;
r -> next = q; r = q;
p = p -> next;
}
r -> next = NULL;
return str;
}


你可能感兴趣的:(编程随手小记)