#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct LIST
{
int element;
LIST *next;
}LIST;
LIST *Init_List (); /** 建表 **/
int Insert_List (LIST *List, int i, int element); /** 插入元素 **/
int Delect_List (LIST *List, int i); /** 删除元素 **/
LIST *Get_piout(LIST *List, int i);
void List_List (LIST *List); /** 打印表 **/
char menu (void);
int main (void)
{
int i;
int element;
LIST *List;
char choice;
List = Init_List ();
do
{
choice = menu ();
switch (choice)
{
case 'i':
case 'I':
printf ("i , element==>> ");
scanf ("%d%d", &i, &element);
Insert_List (List, i, element);
break;
case 'd':
case 'D':
printf ("i==>> ");
scanf ("%d", &i);
Delect_List (List, i);
break;
case 'l':
case 'L':
List_List (List);
break;
default :
break;
}
}while (choice != 'q' && choice != 'Q');
return 0;
}
/**建表**/
LIST *Init_List (void)
{
int i ;
LIST *temp, *This, *List;
List = (LIST *) malloc (sizeof(LIST));
List->next = NULL;
This = List;
/* 表头插入法建有五个元素的表 */
for (i = 1; i <= 5; ++i)
{
temp = (LIST *) malloc (sizeof(LIST));
printf ("enter %d Node>>",i);
scanf ("%d", &(temp->element));
temp->next = NULL;
This->next = temp;
This = temp;
}
return (List);
}
/** 插入表 **/
int Insert_List (LIST *List, int i, int element)
{
LIST *This, *temp;
This = Get_piout (List, i);
temp = (LIST*) malloc (sizeof (LIST));
temp->element = element;
temp->next = This->next;
This->next = temp;
return 0;
}
/*** 删除元素 ***/
int Delect_List (LIST *List, int i)
{
LIST *This, *p;
This = Get_piout (List, i);
p = This->next;
This->next = p->next;
free (p);
return 0;
}
/**** 找位置函数 ***/
LIST *Get_piout(LIST *List, int i)
{
LIST *This;
int j = 0;
This = List;
while ((This != NULL)&& (j < i - 1))
{
This = This->next;
++j;
}
if ((This == NULL) || (j > i - 1))
{
return NULL;
}
return (List) ;
}
/**** 输出函数 ***/
void List_List (LIST *List)
{
LIST *q;
q = List->next;
if(q == NULL)
{
printf ("empty\n");
getch();
return ;
}
while (q->next != NULL)
{
printf ("%d,", q->element);
q = q->next;
}
printf ("%d", q->element);
getch();
}
char menu (void)
{
char choice;
system ("cls");
printf ("----------------------------------\n");
printf (" i insert\n");
printf (" d delete\n");
printf (" l list\n");
printf ("\n\n");
printf (" q qiut\n");
printf ("----------------------------------\n");
printf ("------Your choice>>");
scanf ("%c", &choice);
fflush (stdin);
return (choice);
}