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的元素

二、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 next; } q = (node*)malloc(sizeof(node)); q->data = x; q->next = p->next; p ->next = q;}//只是随便的试了一须添加上stdlib.h。并且有可能还要调试。//建议楼主可以在使用链表的时候画图,然后,思路就清晰了很多。if any question ,call me back!!!

本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。