データ構造の削除操作


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;
}