#include <stdio.h>
#include <stdlib.h> #include <conio.h> #include <malloc.h>#define DATA_TYPE int
typedef struct node { DATA_TYPE data; struct node* next; }LinkList, *Pointer;void menu(); /*显示菜单*/
int readChoice(); /*读菜单选项*/ void cleanBuffer(); /*清除缓存*/ void addElement(); /*添加元素*/ void showElement(); /*显示所有元素*/ Pointer moveToTag(int tag); /*根据tag移动偏移量*/ int removeByPostion(); /*根据位置删除元素*/ int getLinkListSize(); /*获得链表大小*/ void frontToBack(Pointer tail); /*从前向后添加数据*/ void backToFront(Pointer tail); /*从后向前插入元素*/ Pointer createLinkList(); /*从前向后创建链表*/ Pointer moveTail(); /*移动到链表尾部*/ DATA_TYPE findElementByPostion(); /*根据位置查找元素*/Pointer pointer = NULL;
int main(void) { int choice = 0; DATA_TYPE data = -1;menu();
while (1) { printf("\n"); choice = readChoice();switch (choice)
{ case 1: addElement(); printf("add success!!!"); break; case 2: if (removeByPostion()) { printf("Remove success!!!"); } else { printf("Positon error!!"); } break; case 3: data = findElementByPostion(); if (data != -1) { printf("%d", data); } else { printf("haven't data!!"); } break; case 4: showElement(); break; case 5: exit(0); break; default: printf("Option does not match!!!"); break; } }getch();
return 0; }void menu()
{ printf("\t\n#################################\n"); printf("*\t 1.AddElement\t\t*\n"); printf("*\t 2.RemoveByPostion\t*\n"); printf("*\t 3.FindElementByPostion\t*\n"); printf("*\t 4.ShowElement\t\t*\n"); printf("*\t 5.Quit\t\t\t*"); printf("\t\n#################################\n\n"); }int readChoice()
{ int choice = 5; printf("\nPlease input choice:"); while (scanf("%d", &choice) != 1) { printf("Input error!!!\n"); printf("Please input again:"); cleanBuffer(); }return choice;
}void cleanBuffer()
{ int c = 0; while ((c = getchar()) != '\n' && c != EOF ); }void addElement()
{ int o = 0; Pointer tail = NULL;if (pointer == NULL)
{ tail = pointer = createLinkList(); } else { tail = moveTail(); } printf("1.From the front to the back\t"); printf("other.From the back to the front\n"); printf("Please input add data method:"); while (scanf("%d", &o) != 1) { printf("Input error!!!\n"); printf("Please input again:"); cleanBuffer(); }printf("[*Notice : Please enter -1 to end]\n");
if (o == 1) { frontToBack(tail); } else { backToFront(tail); } }Pointer createLinkList()
{ pointer = (Pointer)malloc(sizeof(LinkList)); pointer->next = NULL; pointer->data = 0;return pointer;
}Pointer moveTail() { Pointer current = pointer->next; while (current->next != NULL) { current = current->next; }
return current;
}int getLinkListSize()
{ int count = 0; Pointer current = pointer; while (current->next != NULL) { current = current->next; count++; }return count;
}void showElement()
{ Pointer current = pointer->next; if (current == NULL) { printf("Linklist is empty!!!"); } else { printf("Elements list:\n"); while (current != NULL) { if (current->next == NULL) { printf("%d", current->data); } else { printf("%d-->", current->data); } current = current->next; } }printf("\n");
}Pointer moveToTag(int tag)
{ int i = 0; Pointer current = pointer; while (i++ < tag) { current = current->next; }return current;
}int removeByPostion()
{ int pos = 0; Pointer front = NULL; Pointer rear = NULL; Pointer current = NULL;printf("Please input position:");
while (scanf("%d", &pos) != 1) { printf("Input error!!!\n"); printf("Please input again:"); cleanBuffer(); }if (pos > getLinkListSize() || pos <= 0)
{ return 0; }front = moveToTag(pos - 1);
current = moveToTag(pos); rear = moveToTag(pos+1);front->next = rear;
free(current);return 1;
}DATA_TYPE findElementByPostion()
{ int pos = 0; Pointer current = NULL; printf("Please input position:");while (scanf("%d", &pos) != 1)
{ printf("Input error!!!\n"); printf("Please input again:"); cleanBuffer(); }if (pos > getLinkListSize() || pos <= 0)
{ return -1; }current = moveToTag(pos);
return current->data;
}void frontToBack(Pointer tail) { Pointer current = NULL; while (1) { current = (Pointer)malloc(sizeof(LinkList)); printf("Please input element:"); while (scanf("%d", ¤t->data) != 1) { printf("Input error!!!\n"); printf("Please input again:"); cleanBuffer(); }
if (current->data == -1)
{ break; }current->next = NULL;
tail->next = current; tail = current; } }void backToFront(Pointer tail)
{ Pointer current = NULL; while (1) { current = (Pointer)malloc(sizeof(LinkList)); printf("Please input element:"); while (scanf("%d", ¤t->data) != 1) { printf("Input error!!!\n"); printf("Please input again:"); cleanBuffer(); } if (current->data == -1) { break; } current->next = tail->next; tail->next = current; } }