【数据结构作业】写链表- -

# include<stdio.h>
# include<malloc.h>
# include<stdlib.h>


typedef struct LNode{
  int data;
  struct LNode * next;

}LNode, * LinkList;


LinkList InitList(void);//创建链表
void DestroyList(LNode * pHead);//销毁链表
void ClearList(LNode * pHead);//清空链表
bool ListEmpty(LNode * pHead);//空表返回TRUE,否则FALSE
int ListLength(LNode * pHead);//返回链表长度
void GetElem(LNode * pHead, int i, int *e);//用e返回链表中第i个元素的值
int LocateElem(LNode * pHead, int e);//返回链表中第一个其值与e相等的元素的位序。若不存在返回0
void PriorELem(LNode * pHead, int cur_e, int * a);//若cur_e是链表的元素且不是第一个,则用a返回它的前驱,否则操作失败,a无定义
void NextElem(LNode * pHead, int cur_e, int * a);//若cur_e是链表的元素且不是最后一个,则用a返回它的后继,否则操作失败,a无定义
void ListInsert(LNode *pHead, int i, int e);//在链表第i个元素之前插入新的元素e,链表长度加1(若i==len+1,则就是在链表末尾插入新元素e)
void ListDelete(LNode * pHead, int i, int * e);
void ListTraverse(LNode *pHead);//依次输出链表所有数据元素

int main(void)
{
    LNode * pHead = NULL;
    pHead = InitList();
    //printf("%d\n", LocateElem(pHead, 3));
    int a;
    //NextElem(pHead, 5, &a);
    //ListTraverse(pHead);
    //ListDelete(pHead, 5, &a);
    //ListTraverse(pHead);
    //printf("%d\n", a);
    //PriorELem(pHead, 2, &a);
    //GetElem(pHead, 3, &a);
    //printf("%d\n", a);
    //ListTraverse(pHead);
    //ClearList(pHead);

    return 0;
}

LinkList InitList(void)
{
    int len;
    int i;
    int val;
    LNode * pHead = (LNode *)malloc(sizeof(LNode));
    if(NULL==pHead)
    {
        printf("分配失败,程序终止!\n");
        exit(-1);
    }
    LNode * pTail = pHead;
    printf("请输入您需要生成的链表节点的个数: len = ");
    scanf("%d", &len);
    for(i=0; i<len; i++)
    {
        printf("请输入第%d个节点的值\n", i+1);
        scanf("%d", &val);
        LNode * pNew = (LNode *)malloc(sizeof(LNode));
        if(NULL==pNew)
        {
            printf("分配失败,程序终止!\n");
            exit(-1);
        }
        pNew->data = val;
        pTail->next = pNew;
        pNew->next = NULL;
        pTail = pNew;
    }
    return pHead;
}

void DestroyList(LNode * pHead)
{
  LNode * p = pHead->next;
  while(NULL!=p)
  {
      LNode *q = p;
      p=p->next;
      free(q);
  }
  free(p);
  free(pHead);
}

void ClearList(LNode * pHead)
{
    LNode * p = pHead->next;
    while(NULL!=p)
    {
        LNode * q = p;
        p=p->next;
        free(q);
    }
    pHead->next=NULL;
}

bool ListEmpty(LNode * pHead)
{
    LNode * p=pHead->next;
    if(NULL==p)
    return true;
    else
    return false;
}

int ListLength(LNode * pHead)
{
    int len=0;
    LNode * p = pHead->next;
    while(NULL!=p)
    {
        p=p->next;
        len++;
    }
    return len;
}

void GetElem(LNode * pHead, int i, int * e)
{
    LNode * p = pHead->next;
    while(--i)
    {
        p=p->next;
    }
    *e = p->data;
}

int LocateElem(LNode * pHead, int e)
{
    LNode * p = pHead->next;
    int num=1;
    while(p->next!=NULL&&p->data!=e)
    {
        p=p->next;
        num++;
    }
    if(num==ListLength(pHead))
    return 0;
    else
    return num;

}

void PriorELem(LNode * pHead, int cur_e, int *a)
{
   LNode * p = pHead->next;
   LNode * q;
   while(p&&p->data!=cur_e)
   {
       q=p;
       p=p->next;
   }
   if(p&&p->data==cur_e)
      *a=q->data;
   else
      printf("操作失败\n");
}

void NextElem(LNode * pHead, int cur_e, int * a)
{
   LNode * p =pHead->next;
   while(p&&p->data!=cur_e)
   {
       p=p->next;
   }
   if(p->data==cur_e&&p->next)
   *a=p->next->data;
   else
     printf("操作失败\n");
}

void ListInsert(LNode *pHead, int i, int e)
{
    int len=ListLength(pHead);
    if(len+1<i)
    {
        printf("error\n");
        return;
    }
    LNode *s=(LNode *)malloc(sizeof(LNode));
    s->data=e;
    LNode * p=pHead->next;
    if(i==1)
    {
        pHead->next=s;
        s->next=p;
    }
    else if(i==len+1)
    {
        while(p->next)
        {
            p=p->next;
        }
        p->next=s;
        s->next=NULL;
    }
    else
    {
        i--;
        while(--i)
        {
            p=p->next;
        }
        s->next=p->next;
        p->next=s;
    }
}

void ListDelete(LNode * pHead, int i, int * e)
{
  int len=ListLength(pHead);
  if(i>len||i<1)
  {
      printf("error\n");
      return;
  }
  LNode * p = pHead->next;
  LNode * q;
  if(i==1)
  {
      pHead->next=p->next;
      *e=p->data;
      free(p);
  }
  else
  {
    while(--i)
    {
       q=p;
       p=p->next;
    }
    q->next=p->next;
    *e=p->data;
    free(p);
  }
}

void ListTraverse(LNode *pHead)
{
  LNode * p = pHead->next;
  while(NULL!=p)
  {
      printf("%d ", p->data);
      p = p->next;
  }
  printf("\n");
  return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值