C编程向链表的第pos个位置插入值为x的元素
一、C编程:向链表的第pos个位置插入值为x的元素
#include #include “List.h” template List::List() { m_Head = new Node; m_Head->pNext = NULL; } template List::List(T a[], int n) { m_Head = new Node; m_Head->pNext = NULL; Node *pNode = NULL; int iPos = 0; for (iPos = 0; iPos tData = a[iPos]; pNode->pNext = m_Head->pNext; m_Head->pNext = pNode; } } template List::~List() { Node *pNode = m_Head; Node *ptmpNode = pNode; while (NULL != pNode) { ptmpNode = pNode; pNode = pNode->pNext; delete ptmpNode; } } template int List::Length() { Node *pNode = m_Head; int iLen = 0; while (NULL != pNode->pNext) { iLen++; pNode = pNode->pNext; } return iLen; } template T List::GetData(int iPos) { Node *pNode = m_Head->pNext; int iAt = 1; while ((NULL != pNode) && (iAt pNext; iAt++; } if (NULL == pNode) { printf(“Error: Can’t get the data!\n”); } else { return pNode->tData; } } template int List::Locate(T &x) { Node *pNode = m_Head->pNext; int iPos = 1; while ((NULL != pNode) && (pNode->tData !

二、C编程:向链表的第pos个位置插入值为x的元素
#include
#include “List.h”
template
List::List()
{
m_Head = new Node;
m_Head->pNext = NULL;
}
template
List::List(T a[], int n)
{
m_Head = new Node;
m_Head->pNext = NULL;
Node *pNode = NULL;
int iPos = 0;
for (iPos = 0; iPos < n; iPos++)
{
pNode = new Node;
pNode->tData = a[iPos];
pNode->pNext = m_Head->pNext;
m_Head->pNext = pNode;
}
}
template
List::~List()
{
Node *pNode = m_Head;
Node *ptmpNode = pNode;
while (NULL != pNode)
{
ptmpNode = pNode;
pNode = pNode->pNext;
delete ptmpNode;
}
}
template
int List::Length()
{
Node *pNode = m_Head;
int iLen = 0;
while (NULL != pNode->pNext)
{
iLen++;
pNode = pNode->pNext;
}
return iLen;
}
template
T List::GetData(int iPos)
{
Node *pNode = m_Head->pNext;
int iAt = 1;
while ((NULL != pNode) && (iAt < iPos))
{
pNode = pNode->pNext;
iAt++;
}
if (NULL == pNode)
{
printf(“Error: Can’t get the data!\n”);
}
else
{
return pNode->tData;
}
}
template
int List::Locate(T &x)
{
Node *pNode = m_Head->pNext;
int iPos = 1;
while ((NULL != pNode) && (pNode->tData != x))
{
pNode = pNode->pNext;
iPos++;
}
if (NULL == pNode)
{
printf(“Error: Can’t locate the element!\n”);
}
else
{
return iPos;
}
}
template
void List::Insert(int iPos, T &x)
{
Node *ptmpNode = m_Head;
int iAt = 0;
while ((NULL != ptmpNode) && (iAt < iPos - 1))
{
ptmpNode = ptmpNode->pNext;
iAt++;
}
if (NULL == ptmpNode)
{
printf(“Error: Can’t insert the data!\n”);
}
else
{
Node *pNode = new Node;
pNode->tData = x;
pNode->pNext = ptmpNode->pNext;
ptmpNode->pNext = pNode;
}
}
template
T List::Delete(int iPos)
{
Node *pNode = m_Head;
int iAt = 0;
while ((NULL != pNode) && (iAt < iPos - 1))
{
pNode = pNode->pNext;
iAt++;
}
if ((NULL == pNode) ||(NULL == pNode->pNext))
{
printf(“Error: Can’t delete the node!\n”);
}
else
{
Node *ptmpNode = pNode->pNext;
T tData = ptmpNode->tData;
pNode->pNext = ptmpNode->pNext;
delete ptmpNode;
return tData;
}
}
template
void List::PrintList()
{
Node *pNode = m_Head->pNext;
while (NULL != pNode)
{
printf(“Node is [%d].\n”, pNode->tData);
pNode = pNode->pNext;
}
}
调用Insert方法
三、C编程:向链表的第pos个位置插入值为x的元素
#define typentypedef struct node{ typename data; node * next;void insert(node *list, int pos,typename x){ node * p; node * p = list; int i ; for (i =1; i