001 -- Circle LinkList, list initiate, add a new node, delete a node, and List traverse

#include <studio.h>
#include <stdlib.h>

typedef struct CLinkList
{
    int data;
    struct CLinkList *next ;

} node;

/*initiate a circle list*/

void ds_init(node **pNode)
{
    int item;
    node *temp;
    node *target;

    printf("please input the value of the node, and with 0 input to complete the initiate \n");

    while (1)
    {
        scanf("%d", &item);
        fflush(stdin);

        if(item==0)
            return;
        if ((*pNode)==NULL)
        {
            /*only 1 node in this circle List*/
            *pNode = (node*)malloc(sizeof(struct CLinkList));
            if (!(*pNode))
                exit(0);
            (*pNode)->data = item;
            (*pNode)->next = *pNode;
        }
        else
        {
            /*find the node which next points to*/
            for (target = (*pNode); target->next !=(*pNode); target = target->next )
                ;
            /*create a new Node*/

            temp = (node *)malloc(sizeof(struct CLinkList));

            if (!temp)
                exit(0);

            temp->data = item;
            temp->next = *pNode;
            target->next = temp;
        }
    }
}

/*add new Node*/
/*parameter: first node of the List and the location for adding*/

void ds_insert(node **pNode, int i)
{
    node *temp;
    node *target;
    node *p;
    int item;
    int j = 1;

    printf("please input the location you want to add the node: ");
    scanf("%d". &item);

    if(i == 1)
    { //the new node will be the first one
        temp = (node*)malloc(sizeof(struct CLinkList));
        if(!temp)
            exit(0);

        temp->data = item;

        //look for the last node
        for (target = (*pNode); target->next != (*pNode); target = target->next)
            ;
        temp->next = (*pNode); //new node‘s pointer to the original first node *pNode
        target->next = temp;  //the last node pointer to the new first node temp
        *pNode = temp;     // still, *pNode refer to the first note, which is the newly add one.
    }
    else
    {
        target = *pNode;

        for(;j<(i-1);++j)
        {
            target = target->next;
        }

        temp = (node *)malloc(sizeof(struct CLinkList));

        temp->data = item;
        p = target->next; //original node‘s(locate in i) pointer
        target->next = temp; //target next now become the new node
        temp->next = p; //new node points to the original i node
    }

}

//delete a node

void ds_delete(node **pNode, int i)
{
    node *target;
    node *temp;
    int j = 1;

    if (i==1)
    {
        //delete the first node 

        //find the last node
        for(target=*pNode;target->next!=*pNode;target=target->next)
            ;

        temp = *pNode;
        *pNode = *pNode->next //make sure *pNode points to the first node(original it‘s the seconde node)
        target->next = *pNode; //the last node points to the first one
        free(temp);
    }
    else
    {
        target = *pNode;

        for(;j<i-1;++j)
        {
            target = target->next;
        }

        temp = target->next;
        target->next = temp->next;
        free(temp);

    }

}

//to return the location of the node in the list*/
int ds_search(node *pNode, int elem)
{
    node *target;
    int i = 1;

    for(target = pNode; target!= elem && target->next != *pNode; ++i)
    {
        target=target->next;
    }

    if(target->next == pNode) //means there is no such element in the list
        return 0;
    else
        return i;
}

/*go through the list */
void ds_traverse(node *pNode)
{
    node *temp;
    temp = pNode;
    printf("************Elements in the list**************")

    do
    {
        printf("%4d", temp->data);
    }while((temp=temp->next)!=pNode);

    printf("/n");

}

int main()
{
    node *pHead = NULL;
    char opp;
    int find;
    printf("1. initiate a list\n 2. add a new note \n 3. delete a node \n 4. return location of a given element \n 5. return all elements of the list \n 0. quit \n Please select your actions: ");

    while(opp!=‘0‘)
    {
        scanf("%c", &opp);

        switch(opp)
        {
            case ‘1‘:
               ds_init(&pHead);
               print("\n");
               ds_traverse(pHead);
               break;

            case ‘2‘:
                printf("please input the location of node for adding: ");
                scanf("%d",&find);
                ds_insert(&pHead,find);
                printf("in location %d to add the node: \n", find);
                ds_traverse(pHead);
                print("\n");
                break;
            case ‘3‘:
                printf("please input the location for the node you want to delete: ");
                scanf("%d", &find);
                ds_delete(&pHead,find);
                printf("after delete %d location node: \n", find);
                ds_traverse(pHead);
                print("\n");
                break;
            case ‘4‘:
                printf("which element you want to search for (input the location): ");
                scanf("%d",&find);
                printf("element%d location is: %d\n", find, ds_search(pHead,find));
                print("\n");
                break;
            case ‘5‘:
                ds_traverse(pHead);
                print("\n");
                break;
            case ‘0‘:
                exit(0);

        }
    }
    return 0;
}
时间: 2024-12-30 04:46:48

001 -- Circle LinkList, list initiate, add a new node, delete a node, and List traverse的相关文章

002 -- Circle LinkList 2 -- connect 2 circle link lists

The operation fullfilled codes: // A, B is the rear for 2 LinkList with nodes LinkList Connect(LinkList A,LinkList B) { p = A->next; // use p to store A list head A->next = B->next->next; // A change to point to b1 as the next. free(B->next

App Naver Line 5.3 add new features - &quot;True Delete&quot;

Line is getting more and more popular in recent years. Lots of peope use Line, so do "Suspects". That's why we have to keep an eye on it if any new features added. I just browse Line Engineers' Blog(http://developers.linecorp.com/blog/?p=3660) a

002 -- Circle LinkList 3 -- Puzzels_Magic Poker and Latin

000--Magic Poker Put the poker with specific sequence, so that they can show as below: count 1, open the first card as A, count 2, the first one place to bottom and open the second one as 2; for 3, the first 2 cards put to the bottom, and open the th

Android中的数据结构

数据结构在Android中也有着大量的运用,这里采用数据结构与源代码分析相结合,来认识Android的数据结构 线性表 线性表可分为顺序存储结构和链式存储结构 顺序存储结构-ArrayList 通过对源代码的产看得知,ArrayList继承自AbstractList,实现了多个接口,其中List里面就实现了常用的一些操作,包括增删改查清除大小等等 public class ArrayList<E> extends AbstractList<E> implements List<

LinkList

LinkList:java中jdk1.6之后java.util.LinkList 类中对分装链表的理解: 参考:http://www.cnblogs.com/lintong/p/4374292.html 第一部分:熟悉LinkList中哪些方法和其构造;第二部分熟悉使用JDK中LinkList的API接口 第1部分:LinkList 的介绍: linklist:是一个双向链表,当做堆栈,队列或者双端队列进行操作:当做stack时候只能用push,pop,peek方法:当做队列时候用 add,re

Java集合篇二:LinkList

package com.test.collection; /** * 自定义实现LinkList * * 1.双向链表 * 实现原理:底层封装Node节点对象(每个节点存放有3部分内容:1.上个节点的位置,2.当前节点内容,3.下一个节点的位置) * * 2.查询 * LinkList 相较 ArrayList 查询效率低: * 由于LinkList底层存放元素的不是数组,不能直接通过索引进行获取,需要从头或者从尾逐一遍历索引节点对象. * ArrayList直接通过索引获取即可. * * 3.

002 -- Circle LinkList_Josephus Story

Circle List puzzle Josephus Story: there is 41 people , 39 of them have the agreement that: all these 41 people stand as a circle, and one of them count from 1, and the one count to 3, suicide ! and the people next to him count from 1 again, untill a

Java自己实现双向链表LinkList

/** * <p> * Node 双向链表实体类 * <p> * * @author <a href="mailto:[email protected]">yangkj</a> * @version * @since 2016年8月15日 */ public class Node { // 双向链表-前一节点 Node previous; // 双向链表-当前节点对象 Object obj; // 双向链表-后一节点 Node next;

Java集合之ArrayList与LinkList

注:示例基于JDK1.8版本 参考资料:Java知音公众号 本文超长,也是搬运的干货,希望小伙伴耐心看完. Collection集合体系 List.Set.Map是集合体系的三个接口. 其中List和Set继承了Collection接口. List有序且元素可以重复,默认大小为10:ArrayList.LinkedList和Vector是三个主要的实现类. Set元素不可以重复,HashSet和TreeSet是两个主要的实现类. Map也属于集合系统,但和Collection接口不同.Map是k