2.the data struct on DulLinkList
来源:互联网 发布:sim800 tcp 接收数据 编辑:程序博客网 时间:2024/06/08 03:12
// there are some bugs in the prior one , and it's ok when I run it on my Ubuntu os, just believe in me !
// have a good time!
// copyright vinco zhang
#include<stdio.h>
typedef int ElemType;
typedef struct DulNode
{
ElemType data;
struct DulNode *prior,*next;
}DulNode,* DulLinkList;
/***************function declaration***************************/
void InsertBefore_DuLink_i(DulLinkList L, int i,int x);
void InsertAfter_DuLink_i(DulLinkList L, int i,int x);
void InsertBefore_DuLink(DulLinkList p, int x);
void InsertAfter_DuLink(DulLinkList p, int x);
void DeleteNode_DuLink(DulLinkList p);
DulLinkList Create_DuLink_H(int n);
DulLinkList Create_DuLink_T(int n);
void print_DuLink(DulLinkList L);
void print_DuLink_Reverse(DulLinkList L);
DulLinkList GetElem_DuLink_i(DulLinkList L,int i);
DulLinkList GetElem_DuLink_x(DulLinkList L,int x);
/****************main**************************/
int main()
{
int n;
DulLinkList L,p;
printf("/ninput the length of the DulLinkList n=");
scanf("%d",&n);
printf("/n");
L = Create_DuLink_T(n);
printf("Create_DuLink_T/n");
print_DuLink(L);
//print_DuLink_Reverse(L);
InsertBefore_DuLink_i(L, 3, 100);
printf("InsertBefore_DuLink_i/n");
print_DuLink(L);
//print_DuLink_Reverse(L);
InsertAfter_DuLink_i(L, 3, 200);
printf("InsertAfter_DuLink_i/n");
print_DuLink(L);
//print_DuLink_Reverse(L);
printf("GetElem_DuLink_i/n/n");
p=GetElem_DuLink_i(L,5);
InsertBefore_DuLink(p, 300);
printf("InsertBefore_DuLink/n");
print_DuLink(L);
//print_DuLink_Reverse(L);
InsertAfter_DuLink(p, 400);
printf("InsertAfter_DuLink/n");
print_DuLink(L);
//print_DuLink_Reverse(L);
DeleteNode_DuLink(p);
printf("DeleteNode_DuLink/n");
print_DuLink(L);
//print_DuLink_Reverse(L);
return 0;
}
/*****************function defination*************************/
void InsertBefore_DuLink_i(DulLinkList L, int i,int x)//insert before i th of the L
{
int j=0;
DulLinkList p=L,s;
while(p->next && j<i)// forward to the i th
{
j++;
p=p->next ;
}
s=(DulLinkList)malloc(sizeof(DulNode));
s->data=x;
s->prior=p->prior;
p->prior->next=s;
s->next=p;
p->prior=s;
}
void InsertAfter_DuLink_i(DulLinkList L, int i,int x)// insert after i th of the L
{
int j=0;
DulLinkList p=L,s;
while(p->next && j<i)// forward to the i th
{
j++;
p=p->next ;
}
s=(DulLinkList)malloc(sizeof(DulNode));
s->data=x;
s->next=p->next;
s->next->prior=s;
p->next=s;
s->prior=p;
}
void InsertBefore_DuLink(DulLinkList p, int x)// insert before the point of p
{
DulLinkList s;
s=(DulLinkList)malloc(sizeof(DulNode));
s->data=x;
s->prior = p->prior ;
p->prior->next = s;
s->next=p;
p->prior = s;
p=s;// for do not change p inside the function ???
}
void InsertAfter_DuLink(DulLinkList p, int x)// insert behind the point of p
{
DulLinkList s;
s=(DulLinkList)malloc(sizeof(DulNode));
s->data=x;
s->next=p->next;
s->next->prior=s;
p->next=s;
s->prior=p;
}
void DeleteNode_DuLink(DulLinkList p)// delete the node which position is p
{
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
DulLinkList Create_DuLink_H(int n)// insert into the behind of head node everytime
{
int i;
DulLinkList L = (DulLinkList)malloc(sizeof(DulNode));
if(L==NULL)
{
printf("error when creating head node!/n");
return NULL;
}
L->prior = NULL;
L->next = NULL;
for(i=1;i<=n;i++)
{
DulLinkList s = (DulLinkList)malloc(sizeof(DulNode));
if(s==NULL)
{
printf("error when creating %d node!/n",i);
return L;
}
printf("please input the data of the node:index=%d data=",i);
scanf("%d",&(s->data));
s->next = L->next;
s->prior=L;
L->next = s;
if(s->next)// if n!=1,n=1 means :there is only one node except the head one
{
s->next->prior=s;
}
}
return L;
}
DulLinkList Create_DuLink_T(int n)//insert into the Tail of last node everytime
{
int i;
DulLinkList L = (DulLinkList)malloc(sizeof(DulNode));
DulLinkList p = L;
if(L==NULL)
{
printf("error when creating head node!/n");
return NULL;
}
L->prior = NULL;
L->next = NULL;
for(i=1;i<=n;i++)
{
DulLinkList s = (DulLinkList)malloc(sizeof(DulNode));
if(s==NULL)
{
printf("error when creating %d node!/n",i);
return L;
}
printf("please input the data of the %d node:",i);
scanf("%d",&(s->data));
s->next = NULL;
s->prior =p;
p->next = s;
p = s;
//p->next =NULL;
}
return L;
}
void print_DuLink(DulLinkList L)
{
int i=0;
DulLinkList p=L;
while(p->next!=NULL)
{
i++;
p=p->next ;
printf("index=%d L->data=%d/n",i,p->data );
}
printf("/n");
}
void print_DuLink_Reverse(DulLinkList L)// print the last one firstly
{
int i=0;
DulLinkList p,q=L;
while(q->next)
{
i++;
q=q->next;
}
p=q;// p point to the last one
while(p->prior)// backforward to first one, but not the head one
{
printf("index=%d L->data=%d/n",i,p->data );
p=p->prior ;
i--;
}
printf("/n");
}
DulLinkList GetElem_DuLink_i(DulLinkList L,int i)
{
int j=0;
DulLinkList p=L;
while(p->next && j<i)
{
j++;
p=p->next ;
}
return p;
}
DulLinkList GetElem_DuLink_x(DulLinkList L,int x)
{
DulLinkList p=L->next;
while(p && p->data!=x)
{
p=p->next ;
}
return p;
}
- 2.the data struct on DulLinkList
- the data struct on DulLinkList
- 2. the data struct on LinkList
- the data struct on LinkList
- Data on the Outside vs. Data on the Inside
- Data on the Outside vs. Data on the Inside(译)
- Data Mining on the Web
- STRUCT DATA
- Data Struct
- similarity search on complex data : the direction
- SQL tree data struct(7): find the minimal cost route
- calculate and return the accuracy on the test data
- Designing Data Storage Architecture-the data storage offerings available on the Windows Azure platfo
- CTreeCtrl/CListCtrl/CListBox With ToolTip Based On the Item Data
- Spatial Data on the Web: Modeling and Management
- XMLDM: The Third International Workshop on XML Data Management
- How to display the data from database on JTable
- Proceedings of the Tenth SIAM International Conference on Data Mining
- vi搜索和替换
- Digital Scene mode /数码相机的场景模式
- SQL里有取整函数,四舍五入函数,和截取函数 - eminem
- SQL 一个表中的两个外键来自于同一个表创造的视图
- tomcat环境下服务器文件句柄耗尽(Too Many Open Files)的问题排查
- 2.the data struct on DulLinkList
- List l=new ArrayList()和Arraylist l=new ArrayList()有什么不同啊
- 关于CDC(设备上下文) HDC
- 《现实世界的敏捷开发-大型敏捷研发团队》培训课程扩展阅读
- pxa310 开发环境安装(1)
- java冒泡
- java并发编程-Executor框架详解
- 【操作系统】Bash杂谈
- 程序员从初级到中级10个秘诀--Justin James