⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 实验三参考答案.c

📁 数据结构代码(严为民)
💻 C
字号:
/*  HELLO.C -- Hello, world */

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0

typedef int ElemType;
typedef int Status;

typedef struct LinkedNode{
  ElemType data;
  struct LinkedNode *next;
}LNode;

void about(){       /*版本信息*/
  printf("*******Operation of single linked list**********\n");
  printf("|        Author:DNA     |\n");
  printf("|       Date completed:2007-10-23  |\n");
  printf("*************Welcome to use,^_^**************\n");
    
}

void showmenu(){   /*功能列表*/
   printf("\n ************FUNTION number********\n");
   printf("   * 1.Output all data of linked list!    *\n");
   printf("   * 2.Search for element in linked list! * \n");
   printf("   * 3.Insert element into linked list!   *\n");
   printf("   * 4.Delete element from linked list!   *\n");
   printf("   * 5.End                                *\n");
   printf("   ************************\n");
   printf("Plese inupu the Function number:");
}

/*逆序输入 n 个数据元素,建立带头结点的单链表*/
LNode *CreateList_L(int n) {
 int i;
 LNode *p,*L;
 L=(LNode *)malloc(sizeof(LNode));
 /*if(L==NULL) {printf("Creation fail");exit(0);}*/
 L->next = NULL;      /* 先建立一个带头结点的单链表*/
 printf("Input %d numbers to create a list:\n",n);
for (i = n;i > 0; --i) {
    p =(LNode *)malloc(sizeof(LNode));
    scanf("%d",&p->data);    /* 输入元素值*/
    p->next = L->next;  L->next = p;  /* 插入*/
   }
 return(L);
}

/*******查看输入的全部数据********/
void PrintList(LNode *L){
LNode *p;
 printf("\nData you've inputed are:");
  p=L->next;           /*从头结点开始扫描*/
 while(p){     /*顺指针向后扫描,直到p->next为NULL或i=j为止*/
    printf("%d  ",p->data);
    p=p->next;
    }
 printf("\n");
}


 /*L是带头结点的链表的头指针,以 e 返回第 i 个元素*/
Status GetElem_L(LNode *L, int i, ElemType *e) {
int j;
LNode *p;
p = L->next;   j = 1;  /* p指向第一个结点,j为计数器*/
while (p && j<i)  { p = p->next;  ++j;  }    /* 顺指针向后查找,直到 p 指向第 i 个元素或 p 为空*/
if ( !p || j>i )
    return ERROR;      /*  第 i 个元素不存在*/
*e = p->data;                 /*  取得第 i 个元素*/
return OK;
}

 /*本算法在链表中第i 个结点之前插入新的元素 e*/
Status ListInsert_L(LNode *L, int i, ElemType e) {
int j;
LNode *p,*s;
p = L;    j = 0;
while (p && j < i-1) 
     { p = p->next;  ++j; }   /* 寻找第 i-1 个结点*/
if (!p || j > i-1)
      return ERROR;      /* i 大于表长或者小于1*/
s =(LNode *)malloc(sizeof(LNode));      /* 生成新结点*/
if ( s == NULL)  return ERROR;
s->data = e; 
s->next = p->next;      p->next = s; /* 插入*/
return OK;
}

Status ListDelete_L(LNode *L, int i, ElemType *e)
{LNode *p,*q;
int j;
p = L;    j = 0;
while (p->next && j < i-1) {  p = p->next;   ++j; } 
                           /* 寻找第 i 个结点,并令 p 指向其前趋*/

if  (!(p->next) || j > i-1) 
    return ERROR;  /* 删除位置不合理*/
q = p->next;   p->next = q->next;  /* 删除并释放结点*/
*e = q->data;   free(q);
return OK;
}

void main()
{
 LNode *L;
 int n,choice,i;
  ElemType e;
  about();
  printf("Input the number of linked list you want to creat:");
  scanf("%d",&n);
  L=CreateList_L(n);
 showmenu();  /*功能列表*/
 scanf("%d",&choice);
while(choice!=5)
{ /*输入时候退出程序*/
    switch(choice){
       case 1:PrintList(L);break;        /*1.查看输入的全部数据*/
       case 2:{
           printf("Input the position of the element you want to get: ");
           scanf("%d",&i);GetElem_L(L, i, &e);
           printf("\nthe %dth element is:%d\n",i,e);
           break;}        /*2.查找链表元素*/
       case 3:
           {printf("Input the position of the element you want to insert:");
            scanf("%d",&i);
            printf("\nInsert the element :");
            scanf("%d",&e);
            ListInsert_L(L,i,e);
            break;}       /*3.链表插入元素*/
       case 4:
          {printf("Input the position of the element you want to delete:");
           scanf("%d",&i);
           ListDelete_L(L, i, &e) ;
           break;}        /*4.链表删除元素*/
       default:printf("Wrong Command,Please input again!^_^ ");
    }
 printf("\nPlease input the function number:");
 scanf("%d",&choice);
}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -