您的位置:首页 > 其它

链表的基本操作的实现---- 增删改查

2015-09-15 21:17 357 查看
/**

* 编写:中医熊猫

* 时间:20150801

* 初次编写,还望指教

*/

#include <malloc.h>

#include <stdio.h>

#define LEN sizeof(struct STU)

int n = 0;

struct STU{

int num;

struct STU *next;

};

/**************创建链表********************/

struct STU *creatList()

{

n=0;

struct STU *head;

struct STU *p1,*p2;

head = p1 = p2 = (struct STU *)malloc(LEN);

printf("Please input %d number:",++n);

scanf("%d",&p1->num);

while(p1->num)

{

p1 = (struct STU *)malloc(LEN);

printf("Please input %d number:",++n);

scanf("%d",&p1->num);

p2->next = p1;

p2 = p1;

}

p1->next = NULL;

return head;

}

/********遍历链表**************/

void printList(struct STU *head)

{

printf("\n ergodic chained list:\n");

while(head->num)

{

printf(" %d",head->num);

head = head->next;

}

printf("\n");

}

//插入元素

//在第几个位置上插入一个数。

/*

1.第一个,最后一个。

2.保留前驱。

3.确保后驱。

*/

/***************插入元素***************/

void insertList(struct STU *head)

{

struct STU *p1,*p2;

int i=0;

int number,place;

printf("Please input the number you want to insert:");

scanf("%d",&number);

p1 = (struct STU *)malloc(LEN);

p1->num = number;

printf("Please input the number's lace(0-%d):",n-1);

scanf("%d",&place);

while(head)

{

if(i==place-1)

{

p2->next = p1;

p1->next = head;

printf("insert successfully!");

break;

}

p2 = head;

head = head->next;

i++;

}

}

//删除元素

/********1.删除已知数**************/

void deleteList1(struct STU *head)

{

if(head==NULL)

{

printf("\n it's NULL!!\n");

exit(0);

}

int number;

struct STU *p1,*p2;

printf("\n Please input the number you want to delete:");

scanf("%d",&number);

while(head->num)

{

if(head->num == number)

{

p1->next = head->next;

}

p1 = head;

head = head->next;

}

}

/****** 2.删除指定位置上的数****/

void deleteList2(struct STU *head)

{

if(head==NULL)

{

printf("\n it's NULL!!\n");

exit(0);

}

int i=0,place;

struct STU *p1;

printf("\n Please input the number's place that you want to delete:");

scanf("%d",&place);

while(head->num)

{

if(i++ == place)

{

p1->next = head->next;

printf("\n Luickly...Have delelted\n");

}

p1 = head;

head = head->next;

}

}

/****************改变元素*************************/

/**1.根据值,改变数值*****/

void changeList1(struct STU *head)

{

int number,afternumber;

if(head==NULL)

{

printf("\n it's NULL!!\n");

exit(0);

}

printf("Please input the number you want to change:");

scanf("%d",&number);

printf("After changing ,the number is changed for:");

scanf("%d",&afternumber);

while(head->num)

{

if(head->num == number)

{

printf("after seek ..");

head->num = afternumber;

printf("\nHave changed!!\n");

}

head = head->next;

}

}

/*****2.改变指定位置上的数*****/

void changeList2(struct STU *head)

{

int number,i,j;

if(head==NULL)

{

printf("\n it's NULL!!\n");

exit(0);

}

printf("\n Plaes input the place that number you want to change:");

scanf("%d",&i);

printf("\n After changed,the number is changed for:");

scanf("%d",&number);

j=0;

while(head->num)

{

j++;

if(j==i)

{

head->num = number;

}

head = head->next;

}

}

/*********************查找元素************************/

int seekList(struct STU *head)

{

int number,i=1;

if(head==NULL)

{

printf("\n it's NULL!!\n");

exit(0);

}

printf("\n Please input the number you want to seek:");

scanf("%d",&number);

while(head->num)

{

if(head->num == number)

{

printf("\n seek successfully! the place is %d\n",i);

}

head = head->next;

i++;

}

}

/*****************************Menu***************************/

void printMenu()

{

printf("\n\t***********************************************************\n");

printf("\t* THE MENU FOLLOW *\n");

printf("\t* 0.print the Menu *\n");

printf("\t* 1.creat Linked List *\n");

printf("\t* 2.insert the Element to List *\n");

printf("\t* 3.delete the Element to List according to number *\n");

printf("\t* 4.delete the Element to List according to place *\n");

printf("\t* 5.change the Elemnet to List according to number *\n");

printf("\t* 6.change the Element to List according to place *\n");

printf("\t* 7.seek the list *\n");

printf("\t* 8.ergodic chained list *\n");

printf("\t* 9.exit *\n");

printf("\t************************************************************\n");

}

/****************主函数******************/

int main(void)

{

int num;

struct STU *head;

printMenu();

printf("\norder:");

while(scanf("%d",&num)!=EOF)

{

switch(num)

{

case 0:printMenu();break;

case 1:head=creatList();break;

case 2:insertList(head);break;

case 3:deleteList1(head);break;

case 4:deleteList2(head);break;

case 5:changeList1(head);break;

case 6:changeList1(head);break;

case 7:seekList(head);break;

case 8:printList(head);break;

case 9:exit(0);

}

printf("order:");

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: