链表

 

/* 头结点的数据域用来存放链表长度*/

#include<stdio.h>
#include<stdlib.h>
///定义数据
typedef int ElemType;
typedef struct LNode
{
 struct LNode *next;
 ElemType data;
}LNode;
///

///初始化操作
void InitList(LNode *L)
{
 L->data=0;
 L->next=0;
}
///

///建立新表
void CreateList(LNode *L)
{
 int n;
 
 printf("How many LinkList do you want to Add:");
 scanf("%d",&n);
 L->data+=n;
 printf("Please Input %d Element:\n",n);
 for(int i=0;i<n;i++)
 {
  while(L->next) L=L->next;
  LNode *p=(LNode*)malloc(sizeof(LNode));
  scanf("%d",&p->data);
  L->next=p;
  p->next=NULL;
 }
}
///

///输出链表

void PrintList(LNode *L)
{
 printf("The Length of the List is:%d\n",L->data);
 while(L->next)
 {
  L=L->next;
  printf("%d ",L->data);
 }
 printf("\n");
}
///

///插入新表
void InsertList(LNode *L)
{
 int n,j=1;
 printf("Please Input The Position:");
 scanf("%d",&n);
 if(n<1 || n>L->data)
 {
  printf("Position ERROR!\n");
  exit(0);
 }
 L->data++;
 while(L->next && j<n)
 {
  L=L->next;
  j++;
 }
 LNode *p=(LNode*)malloc(sizeof(LNode));
 printf("Please Input the Element you want to Insert:");
 scanf("%d",&(p->data));
 p->next=L->next;
 L->next=p;
}
///

///删除链表
void DeleteList(LNode *L)
{
 printf("Please Input the Position of List you want to Delete:");
 int n,i=1;
 scanf("%d",&n);
 if(n<1 || n>L->data)
 {
  printf("Position ERROR!\n");
  exit(0);
 }
 L->data--;
 while(L->next && i<n)
 {
  L=L->next;
  i++;
 }
 LNode *p;
 p=L->next;
 L->next=p->next;
 free(p);
}
///

///查找-元素
void LocatList_Elem(LNode *L)
{
 printf("Please Input the Element you want to Find:");
 ElemType e;
 scanf("%d",&e);
 int i=1,flag=0;
 while(L->next)
 {
  if(L->data == e)
  {
   printf("%d ",i);
   flag=1;
  }
  L=L->next;
 }
 printf("\n");
 if(!flag) printf("NOT Find!\n");
}
///

///查找-位置
void LocatList_Position(LNode *L)
{
 printf("Please Input the Position you want to Find:");
 int n,i=1;
 scanf("%d",&n);
 if(n<1 || n>L->data)
 {
  printf("Position ERROR!\n");
  exit(0);
 }
 while(L->next && i<=n)
 {
  L=L->next;
  i++;
 }
 printf("%d\n",L->data);
}
///

///主菜单
int Select()
{
 printf("\n\t\t1.Create a New LinkList.\n");
 printf("\t\t2.Print the LinkList.\n");
 printf("\t\t3.Insert a Link.\n");
 printf("\t\t4.Delete a Link.\n");
 printf("\t\t5.Find a Link's Element by Position.\n");
 printf("\t\t6.Find a Link's Position by Element.\n");
 printf("\t\t7.Destory LinkList.\n");
 printf("\t\t8.Exit.\n");
 printf("\tPlease Input your chiose:");
 int select;
 scanf("%d",&select);
 return select;
}
///

///判断表是否存在
int Exist(LNode *L)
{
 if(L->next && L->data>0) return 1;
 else printf("LinkList hasn't be Created! Please Create a LinkList first!\n");
 return 0;
}
///
///销毁表
void DestoryList(LNode *L)
{
 LNode *p;
 L->data=0;
 L=L->next;
 p=L;
 while(L->next)
 {
  L=L->next;
  free(p);
  p=L;
 }
 printf("LinkList has been destoryed!\n");
}
///
///主函数
int main()
{
 LNode *head=(LNode*)malloc(sizeof(LNode));
 printf("\t\t\t LINK LIST\n\n");
 while(true)
 {
  switch(Select())
  {
  case 1: 
   {
    InitList(head);
    CreateList(head);
    printf("LinkList has been Created!\n");
    break;
   }
  case 2:
   {
    if(Exist(head)) PrintList(head);
    break;
   }
  case 3:
   {
    if(Exist(head)) InsertList(head);
    break;
   }
  case 4: 
   {
    if(Exist(head)) DeleteList(head);
    break;
   }
  case 5:
   {
    if(Exist(head)) LocatList_Position(head);
    break;
   }
  case 6:
   {
    if(Exist(head)) LocatList_Elem(head);
    break;
   }
  case 7:
   {
    if(Exist(head)) DestoryList(head);
    break;
   }
  case 8:
   {
    printf("Thanks for Using ! Good - bye!\n");
    return 0;
   }

  default:
   {
    printf("ERROR! Input Again!\n");
    break;
   }
  }
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值