标题:我做的实验 关于双向循环链表的操作
只看楼主
撒旦
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-1
 问题点数:0 回复次数:0 
我做的实验 关于双向循环链表的操作
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    int data;
    struct node *prior,*next;
}Node;
Node* createnode(Node *head)
{
    head=(Node *)malloc(sizeof(Node));
    head->prior=head->next=NULL;
    return head;
}
Node* Findnode(Node *head,int i)
{
    Node *p;
    int j=1;
    p=head;    
    if(p->next==head)
        return NULL;
    while(j<i)
    {
       p=p->next;
       j++;
    }
    return p;
}
int insert(Node *head,int i,int value)
{
   
    Node *p,*q,*r;
    p=(Node *)malloc(sizeof(Node));
    p=head;
    if(p->next==NULL)
    {
        q=(Node *)malloc(sizeof(Node));
        q->data=value;
        p->next=q;
        q->next=p;
        p->prior=q;
        q->prior=p;
        return 0;
    }
    else
    {   
        q=(Node *)malloc(sizeof(Node));
        q=Findnode(head,i);
        r=(Node *)malloc(sizeof(Node));
        r->data=value;
        r->prior=q;
        r->next=q->next;
        q->next->prior=r;
        q->next=r;
        return 0;
    }
}
void Deletenode(Node *head,int i)
{
    Node *q;
    q=Findnode(head,i);
    q->prior->next=q->next;
    q->next->prior=q->prior;
    free(q);
}
void Displist(Node *head)
{
    Node *p=head->next;
    while(p!=head)
    {
        printf("%3d",p->data);
        p=p->next;
    }
    printf("\n");
}
void main()
{
    int m,n,k,i,j=11;
    Node *head=NULL;
    head=createnode(head);
    printf("初始化链表\n");
    for(i=1;i<13;i++)
    {
      insert(head,i,j);
      j++;
    }
    printf("show the list\n");
    Displist(head);
    printf("Input you want to insert to:(结点号和元素值) ");
    scanf("%d,%d",&m,&n);
    insert(head,m,n);
    Displist(head);
    printf("Input you want to delete to:(结点号) ");
    scanf("%d",&k);
    Deletenode(head,k);
    Displist(head);
}

u=184641577,1927244&gp=-26.jpg (5.12 KB)
搜索更多相关主题的帖子: Node 链表 head int 实验 
2007-12-26 22:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-194498-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.263956 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved