在前文实现单向链表的基本操作下,本文实现双向链表的基本操作.

双向链表与单链表差异,是双向链表结点中有前向指针和后向指针.所以在插入和删除新结点元素时候不见要考虑后向指针还要考虑前向指针.

以下是双向链表的C代码:

 #include<stdio.h>

 typedef struct node
{
int data;
struct node *next;
struct node *prior
}Node; //链表的初始化
Node* InitList(int number)
{
int i;
Node *pHead=(Node *)malloc(sizeof(Node));
Node *TempHead=pHead;
Node *Head=pHead;
Head->prior=NULL;
int data;
for(i=;i<number;i++)
{
pHead=(Node *)malloc(sizeof(Node));
printf("Please input the %dst node data:\n",i+);
scanf("%d",&data);
pHead->data=data;
pHead->next=NULL;
pHead->prior=TempHead;
TempHead->next=pHead;
TempHead=pHead;
}
return Head;
} //显示链表
void ShowList(Node *Head)
{ Head=Head->next;
while(Head->next!=NULL)
{
printf("%d ",Head->data);
Head=Head->next;
}
printf("%d",Head->data);
printf("\n");
} //输出链表某个值的位置
int ListLocation(Node *Head,int data,int number)
{
Node *TempNode=Head;
int location=;
TempNode=TempNode->next;
while(TempNode->next!=NULL)
{
if(TempNode->data==data)
{
return location;
}
location++;
TempNode=TempNode->next;
}
if(location>=number)
printf("Not found!");
} //输出链表某个位置的值
int ListData(Node *Head,int location,int number)
{
if(location>number)
printf("Not found!"); Node *TempNode=Head;
TempNode=TempNode->next;
int i;
for(i=;i<=number;i++)
{
if(location==i)
return TempNode->data;
TempNode=TempNode->next;
}
} //头入法插入元素
void HeadInsertData(Node *Head,int data)
{
Node *InsertNode=(Node *)malloc(sizeof(Node));
InsertNode->data=data;
InsertNode->next=Head->next;
Head->next->prior=InsertNode;
Head->next=InsertNode;
InsertNode->prior=Head;
} //尾入插入除元素
void TailInsertData(Node *Head,int data)
{
Node *TempNode=Head;
Node *DeleteNode=(Node *)malloc(sizeof(Node));
DeleteNode->data=data;
while(TempNode->next!=NULL)
TempNode=TempNode->next; TempNode->next=DeleteNode;
DeleteNode->next=NULL;
DeleteNode->prior=TempNode; free(DeleteNode);
} //删除头结点
void HeadDeleteData(Node *Head)
{
Head->next=Head->next->next;
Head->next->prior=Head;
} //删除尾结点
void TailDeleteData(Node *Head)
{
Node *TempNode=Head;
while(Head->next->next!=NULL)
Head=Head->next; Head->next=NULL;
Head->next->prior=NULL;
} int main()
{
Node *Head;
int number;
printf("Please input the node number:\n");
scanf("%d",&number);
Head=InitList(number);
printf("The initital list is:\n");
ShowList(Head); int flag;
printf("\n\n");
printf("**********************Your Choice********************\n");
printf("****************1-输出链表某个值的位置***************\n");
printf("****************2-输出链表某个位置的值***************\n");
printf("****************3-头入法插入元素*********************\n");
printf("****************4-尾入法插入元素*********************\n");
printf("****************5-删除头结点*************************\n");
printf("****************6-删除尾结点*************************\n");
printf("****************0-退出*******************************\n");
printf("\n\n");
printf("Please input flag:\n");
scanf("%d",&flag); switch(flag)
{
case :
{
int data;
printf("Please input the data you want locate:\n");
scanf("%d",&data);
int location;
location=ListLocation(Head,data,number);
printf("The data's location is: %d",location);
break;
}
case :
{
int location;
printf("Please input the location you want data:\n");
scanf("%d",&location);
int data;
data=ListData(Head,location,number);
printf("The location's data is: %d\n",data);
break;
}
case :
{
int data;
printf("Please input the data you want insert in head:\n");
scanf("%d",&data);
HeadInsertData(Head,data);
ShowList(Head);
break;
}
case :
{
int data;
printf("Please input the data you want insert in tail:\n");
scanf("%d",&data);
TailInsertData(Head,data);
ShowList(Head);
break;
}
case :
{
HeadDeleteData(Head);
ShowList(Head);
break;
}
case :
{
TailDeleteData(Head);
ShowList(Head);
break;
}
case :
{
printf("You choose to exit.\n");
break;
}
}
return ;
}

结果图:

 

最新文章

  1. Hawk 6. 高级话题:子流程系统
  2. Effective java笔记(五),枚举和注解
  3. 创建Unicode格式的INI文件
  4. Intellij IDEA中使用Struts2
  5. listen和accept函数
  6. Unity3D开发(五):Unity3D 4.x 使用Mecanim实现连击(转)
  7. Git CMD - add: Record changes to the repository
  8. hdu 3072
  9. Java ------------获取不会重复的随机数
  10. [11-2] adaboost理解
  11. NSUserDefaults设置bool值重新启动后bool仅仅设置丢失问题
  12. JObject对json的操作
  13. windows上使用SecureCRT连接linux
  14. JS合并两个数组的方法
  15. Int2BinaryString.java
  16. getitem, setitem, delitem (把类实例化成字典的类型)
  17. grpc 实现微服务生态笔记
  18. 牛客网——G送分了(py)
  19. Java中23种设计模式(附代码样例)
  20. angularJs上传文件(非form上传)

热门文章

  1. List&lt;T&gt;类
  2. [PowerShell] Backup Folder and Files Across Network
  3. 狗屎的asp.net core 果然发生了重大的变更
  4. UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures&lt;Problem H&gt;
  5. cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
  6. Zookeeper 5、Zookeeper应用场景
  7. Unity 制作RPG地图2(自己控制地图上图标)
  8. 棋盘覆盖(大数阶乘,大数相除 + java)
  9. svn 清理失败 (cleanup 失败) 的解决方法
  10. Hacker(五)----黑客专用通道---&gt;端口