#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
typedef struct list
{
int data;
struct list* next;
}LIST;
void add(int data);
LIST* findBack();
void insertBack(LIST *p);
void insert(int position,int data);
void discs(int data);
void returnvalue();
void del(int position);
void change(int position,int data);
void showMenu();
LIST head;
char ch;
int count=0;
int position;
int main()
{
int select;
head.next=NULL;
while(1)
{
showMenu();
printf("请选择需要的操作:");
scanf("%d",&select);
fflush(stdin);
switch(select)
{
case 1:
case 2:
case 3:
case 4:
case 0:exit(1);
default:printf("输入错误!\n");
}
system("pause");
}
return 0;
}
void add(int data)
{
LIST *p;
p=(LIST *)malloc(sizeof(LIST));
count++;
if(p == NULL)
{
printf("动态内存分配失败!");
return;
}
p->data = data;
insertBack(p);
}
LIST* findBack()
{
LIST *p;
p=&head;
while(p->next!=NULL)
{
p=p->next;
}
return p;
}
void insertBack(LIST *p)
{
LIST *tail;
tail = findBack();
tail->next = p;
p->next = NULL;
return ;
}
void change(int position,int data)
{
LIST *p;
p=&head;
while(position)
{
p = p->next;
position--;
}
p->data = data;
}
void insert(int position,int data)
{
LIST *p,*pre;
p=(LIST *)malloc(sizeof(LIST));
if(position>(count+1))
{
printf("超出插入位置范围!\n");
system("pause");
return;
}
pre=&head;
position--;
while(position)
{
if(p!=NULL)
{
position--;
pre = pre->next;
}
}
if(p!=NULL)
{
p->next = pre->next;
pre->next = p;
p->data = data;
}
}
void del(int position)
{
LIST *p,*pre;
pre=&head;
p=head.next;
if(position < 0||position > count)
{
printf("你删除的位置不存在\n");
system("pause");
return;
}
position--;
while(position)
{
position--;
pre = pre->next;
p = p->next;
}
if(p!=NULL)
{
pre->next = p->next;
free(p);
}
}
void returnvalue()
{
LIST *p,*q,*headp;
headp=&head;
p=&head;
p = p->next;
while(p->next != NULL)
{
q = p->next;
p->next = q->next;
q->next = headp->next;
headp->next = q;
}
p->next =headp;
headp =p->next;
p->next = NULL;
printf("反转成功!\n");
system("pause");
}
void showMenu()
{
system("cls");
return;
}