双向循环链表 查找元素或元素指针

查找元素
如果看不懂 建议去看看我的 单链表
加油 入门后简直快活

//**双向循环链表  查找元素或元素指针**
#include
#include
typedef struct node
{
    int data;
    struct node *next, *pre;
}link;
//生成该链表
link *creat(link *head,int n)
{
    link *p, *q;
    //初始化
    p= head = (link*)malloc(sizeof(link));
    head->pre = head;
    head->next = head;

    while(n--)
    {
        q= (link*)malloc(sizeof(link));
        printf("输入一个数据:");
        scanf("%d",&q->data);
        p->next = q;
        q->pre =p;
        q->next = head;
        head->pre = q;
        p =q;
    }
    return head;
}
link *get(link *head,int i)
{
    link *p=head;

    while(i!=0)
    {
        i--;
        p = p->next;
    }
    printf(" 该元素为:%d\n",p->data);
    return p;
}
void main()
{
    link *head, *target;
    int n, i;
    printf("有几个数据?");
    scanf("%d",&n);
    head = creat(head,n);
    printf("你要查找第几个元素?\n");
    scanf("%d",&i);
    target = get(head,i);
}

你可能感兴趣的:(C语言)