当前位置:编程学习 > C/C++ >>

C编程:向链表的第pos个位置插入值为x的元素

答案:#define typename int typedef struct node{ typename data; node * next; } node; void insert(node *list, int pos,typename x) { node * p; node * q; p = list; int i ; for (i =1; i <pos; i++ ) { p = p ->next; } q = (node*)malloc(sizeof(node)); q->data = x; q->next = p->next; p ->next = q; } //只是随便的试了一下算法,在主函数中必须添加上stdlib.h。并且有可能还要调试。 //建议楼主可以在使用链表的时候画图,然后,思路就清晰了很多。 if any question ,call me back!!!
其他:#include <stdio.h>

#include "List.h"

template <class T>
List<T>::List()
{
    m_Head = new Node<T>;
    m_Head->pNext = NULL;
}

template <class T>
List<T>::List(T a[], int n)
{
    m_Head = new Node<T>;
    m_Head->pNext = NULL;

    Node<T> *pNode = NULL;
    int iPos = 0;
    for (iPos = 0; iPos < n; iPos++)
    {
        pNode = new Node<T>;
        pNode->tData = a[iPos];

        pNode->pNext = m_Head->pNext;
        m_Head->pNext = pNode;
    }
}

template <class T>
List<T>::~List()
{
    Node<T> *pNode = m_Head;
    Node<T> *ptmpNode = pNode;

    while (NULL != pNode)
    {
        ptmpNode = pNode;
        pNode = pNode->pNext;
        delete ptmpNode;
    }
}

template <class T>
int List<T>::Length()
{
    Node<T> *pNode = m_Head;
    int iLen = 0;

    while (NULL != pNode->pNext)
    {
        iLen++;
        pNode = pNode->pNext;
    }

    return iLen;
}

template <class T>
T List<T>::GetData(int iPos)
{
    Node<T> *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 <class T>
int List<T>::Locate(T &x)
{
    Node<T> *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 <class T>
void List<T>::Insert(int iPos, T &x)
{
    Node<T> *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<T> *pNode = new Node<T>;
        pNode->tData = x;

        pNode->pNext = ptmpNode->pNext;
        ptmpNode->pNext = pNode;
    }
}

template <class T>
T List<T>::Delete(int iPos)
{
    Node<T> *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<T> *ptmpNode = pNode->pNext;
        T tData = ptmpNode->tData;
        pNode->pNext = ptmpNode->pNext;
        delete ptmpNode;

        return tData;
    }
}

template <class T>
void List<T>::PrintList()
{
    Node<T> *pNode = m_Head->pNext;

    while (NULL != pNode)
    {
        printf("Node is [%d].\n", pNode->tData);
		pNode = pNode->pNext;
    }
}

调用Insert方法 struct student *create(int n) { int i; struct student *head=NULL, 反正是给你量身定做的代码呵呵 #include

上一个:c语言基础问题
下一个:新手自学C语言,要怎么入手?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,