Career Cup 2-3

/*******************************************************

    2.3
    Implement an algorithm to delete a node in the middle of a single linked list, given
    only access to that node
    EXAMPLE
    Input: the node ‘c’ from the linked list a->b->c->d->e
    Result: nothing is returned, but the new linked list looks like a->b->d->e

    Complexity: O(n)

  Mod Time: 5.6, 2012

  Copyright: Ben

  *******************************************************/


#include<stdio.h>
#include<malloc.h>

#define OK                    0
#define ERROR                -1

#define MYDEBUG                0

#define LASTN                13        //last nth element.

typedef int ElemType;

typedef int Status;

struct Node;
typedef Node ListNode;
typedef ListNode * Position;
typedef Position LinkList;

struct Node
{
    ElemType elem;    //this field of header should be NULL.
    Position next;
};

LinkList New_LinkList(void);
Status Print_LinkList(LinkList ll);
Status Append_LinkList(LinkList ll, ElemType val);
Status Destroy_LinkList(LinkList ll);
ElemType DeleteNextNode(Position p);

int main(void)
{
    int readInt, Loca;
    int NumOfElem = 0;    //counter of how many elements.
    int i;
    ElemType val;
    //ElemType elem;
    Position TravNode;
    LinkList list = New_LinkList();
    if(!list)
    {
        return ERROR;
    }

    printf("Please input the No. of element that should be deleted.\n");
    scanf("%d", &Loca);
    printf("%d", Loca);
    
    freopen("array.txt", "r", stdin);

    while(1)
    {
        if(scanf("%d", &readInt) == EOF)
        {
            break;
        }
        Append_LinkList(list, readInt);
        NumOfElem++;
    }

    Print_LinkList(list);

    if(Loca > NumOfElem)
    {
        printf("Impossible to delete that node!\n");
        return ERROR;
    }
    else //if(Loca < NumOfElem)
    {
        TravNode = list;
        for(i = 1; i < Loca; i++)
        {
            TravNode = TravNode->next;
        }
        val = DeleteNextNode(TravNode);
        printf("The deleted element is %d\n", val);
    }
    /*
    else        // delete the link tail.
    {
        
    }*/    

    Print_LinkList(list);

    fclose(stdin);

    

    Destroy_LinkList(list);

    return OK;
}

LinkList New_LinkList(void)
{
    LinkList ll = (LinkList)malloc(sizeof(ListNode));
    ll->elem = 0;
    ll->next = NULL;
    return ll;
}

Status Print_LinkList(LinkList ll)
{
    Position p = ll;

    if(!ll)    //trying to delete a linklist that does not exist
    {
        printf("trying to print a linklist that does not exist\n");
        return ERROR;
    }
    printf("The elements in link list: \n");
    while(p->next != NULL)
    {
        p = p->next;
        printf("%d\t", p->elem);
    }
    return OK;
}

Status Append_LinkList(LinkList ll, ElemType val)
{
    Position posi;
    if(!ll)    //trying to delete a linklist that does not exist
    {
        printf("trying to insert to a linklist that does not exist\n");
        return ERROR;
    }
    posi = ll;
    while(posi->next)
    {
        posi = posi->next;
    }

    posi->next = (Position)malloc(sizeof(ListNode));
    posi->next->next = NULL;
    posi->next->elem = val;

    return OK;
}

Status Destroy_LinkList(LinkList ll)
{
    Position posi, temp;
    if(!ll)    //trying to delete a linklist that does not exist
    {
        printf("trying to delete a linklist that does not exist\n");
        return ERROR;
    }
    
    posi = ll;
    ll->next = NULL;

    while(posi->next)
    {
        temp = posi->next;
        free(posi);
        posi = temp;
    }
    return OK;
}

ElemType DeleteNextNode(Position p)
{
    ElemType val;
    Position temp = p->next;
    val = temp->elem;
    p->next = p->next->next;
    free(temp);
    return val;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值