双向链表

#include <stdio.h>

#include <conio.h>
#include <windows.h>
#include <stdlib.h>

#define LEN sizeof(struct node)

typedef struct node * snode;

struct node 
{
char name[10];
snode pleft,pright;
};

snode create(int n) //create double link...
{
snode ptr,head,newnode;
head=(snode)malloc(LEN);
newnode=(snode)malloc(LEN);

head->name[0]='/0';
head->pright=NULL;
head->pleft=NULL;
ptr=head;

if(head==NULL)
{
printf("No enough space!");
exit(0);
}

for(int i=0;i<n;i++)
{
if(newnode==NULL)
{
printf("Sorry!");
exit(0);
}

ptr->pright=newnode;
printf("enter names:");
scanf("%s",head->name);

newnode->pleft=ptr;
newnode->pright=NULL;
ptr=newnode;
}
head->pleft=newnode;
ptr->pright=head;
return head;
}

snode search(snode head,char * selement) //search double link...
{
snode ptr;
char * temp;
ptr=head->pright;
while(head!=ptr)
{
temp=ptr->name;
if(strcmp(temp,selement)==0)
{
return ptr;
}
else
{
ptr=ptr->pright;
}
}
printf("Not found!");
Sleep(3000);
return NULL;
}

void insert(snode head,char * previous,char * insertelement) //insert double link...
{
snode ptr,newnode;
//printf("Before which element you want to insert!");
if((ptr=search(head,previous))==0)
{
printf("No space to insert!");
exit(0);
}
if((newnode=(snode)malloc(LEN))==NULL)
{
printf("No enough space!");
exit(0);
}

strcpy(newnode->name,insertelement);
ptr->pleft->pright=newnode; //a->c;
newnode->pleft=ptr->pleft; //c->a;
newnode->pright=ptr; //c->b;
ptr->pleft=newnode; //b->c;
// return NULL;
}

void del(snode deletelink) //delete double link...
{
deletelink->pleft->pright=deletelink->pright;
deletelink->pright->pleft=deletelink->pleft;
free(deletelink);
}

void print(snode head) //print double link...
{
snode ptr;
ptr=head->pright;
while(ptr!=head)
{
printf("%s",ptr->name);
ptr=ptr->pright;
}
printf("Print success!");
Sleep(3000);
}

void main()
{
//自己按照需要添加
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值