| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 829 人关注过本帖
标题:我做的实验 关于双向循环链表的操作
只看楼主 加入收藏
撒旦
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
快速回复:我做的实验 关于双向循环链表的操作
数据加载中...
 
   



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

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