徐州文职一般工资多少:一道有关数据结构的题目!(帮帮忙!)

来源:百度文库 编辑:神马品牌网 时间:2024/05/04 19:00:44
// 2_3.cpp : Defines the entry point for the console application.
/*2.3 实现双链表各种基本运算的算法
编写一个程序,实现双链表的各种基本运算,并在此基础上设计一个主程序完成如下功能:
(1 ) 初始化双链表h。
(2 ) 依次采用尾插法插入a,b,c,d,e元素。
(3 ) 输出双链表h。
(4 ) 输出双链表h的长度。
(5 ) 判断双链表h是否为空。
(6) 输出双链表h的第3个元素。
(7) 输出元素a的位置。
(8) 在第4个元素位置上插入f元素。
(9) 输出双链表h。
(10) 删除双链表h的第3个元素。
(11) 输出双链表h。
(12) 释放双链表h。
*/

#include "stdafx.h"
#include "malloc.h"

#define true 1
#define false 0
#define ok 1
#define error 0
#define infeasible -1
#define overflow -2
typedef int Status;
typedef char elemtype;

typedef struct DuLNode
{
elemtype data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode, *DuLinkList;

Status InitList_DuL(DuLinkList &L)
{
L=(DuLinkList)malloc(sizeof(DuLNode));
L->prior=L->next=NULL;
return ok;
}

DuLinkList GetElemP_DuL(DuLinkList &L,int i)
{
DuLinkList p;

int j;
p=L->next;
j=1;
while(p && j<i)
{
p=p->next;
++j;
}
if(!p||j>i) return NULL;

return p;
}

Status ListInsert_DuL(DuLinkList &L,int i,elemtype e)
{
DuLinkList s;
DuLinkList p;
if(!(p=GetElemP_DuL(L,i)))
return error;

if(!(s=(DuLinkList)malloc(sizeof(DuLNode)))) return error;
s->data=e;
s->prior=p->prior; p->prior->next=s;
s->next=p; p->prior=s;
return ok;
}

Status DisplayList_DuL(DuLinkList &L)
{
DuLinkList p;
p=L->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
return ok;
}

Status ListLength_DuL(DuLinkList &L)
{
DuLinkList p;
int i;
p=L;
i=0;
while(p->next!=NULL)
{
i++;
p=p->next;
}
return(i);
}

void main()
{
DuLinkList h;
printf("构造一个空的双链表h.\n");
InitList_DuL(h);
printf("依次采用尾插法插入a,b,c,d,e元素\n");
ListInsert_DuL(h,1,'a');
ListInsert_DuL(h,2,'b');
ListInsert_DuL(h,3,'c');
ListInsert_DuL(h,4,'d');
ListInsert_DuL(h,5,'e');
//printf("输出双链表h.");
// DisplayList_DuL(h);
printf("输出双链表h的长度.");
ListLength_DuL(h);
printf("(4)=%d\n",ListLength_DuL(h));

}