#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdbool.h>
typedef int elementType;
typedef struct node
{
int data;
struct node1*next;
} LinkList;
FILE *fp;
LinkList*Creat(int length)
{
LinkList*head,*p,*q;
int i,j;
int a[length];
fp=fopen("D:\\SP\\sp.txt","r");
if(fp==NULL)
{
printf("The file can not be opened!");
exit(1);
}
for(i=0; i<length; i++)
{
fscanf(fp,"%d",&a[i]);
}
fscanf(fp,"\n");
fclose(fp);
head=(LinkList*)malloc(sizeof(LinkList));
head->next=NULL;
q=head;
for(j=0; j<length; j++)
{
p=(LinkList*)malloc(sizeof(LinkList));
q->next=p;
p->data=a[j];
q=p;
}
p=(LinkList*)malloc(sizeof(LinkList));
q->next=p;
q->next=NULL;////////////////////////////////////////////////////q-next 而不是q
return head;
}
LinkList*Sort(LinkList*head){
if(head==NULL){
return NULL;
}
LinkList*p=head->next;
LinkList*p_pre=p;
bool flag=false;
while(p_pre!=NULL){
int min=p_pre->data;
p=p_pre->next;
while(p){
if(min<=p->data){
p=p->next;
flag=true;
continue;
}
else
{
min=p->data;////////////////////////先赋值给min,不然后面的值是交换了的!
int temp;
temp=p_pre->data;
p_pre->data=p->data;
p->data=temp;
p=p->next;
flag=true;
}
if(flag==false){
return head;
}
}
p_pre=p_pre->next;
}
return head;
}
void Display(LinkList*head)
{
LinkList*p;
p=head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int Search(LinkList*head,int x)
{
int n=0;
LinkList *p;
p=head->next;
while(p!=NULL&&(p->data!=x))
{
p=p->next;
n++;
}
if(p==NULL)
{
return -1;
}
else
{
return n;
}
}
LinkList*Insert(LinkList*head,int insertnum){
LinkList*p,*q,*c,*d;
c=(LinkList*)malloc(sizeof(LinkList));
d=(LinkList*)malloc(sizeof(LinkList));
p=head;
q=p->next;
if(q==NULL){
printf("The list is empty!");
}
if(q->data>=insertnum){
c->data=insertnum;
c->next=head->next;
return head;
}
else{
p=q;
q=p->next;
while(q!=NULL){
if((q->data)>=insertnum){
c->data=insertnum;
c->next=p->next;
p->next=c;
return head;
}
else{
p=q;
q=p->next;
}
if(q==NULL){
c->data=insertnum;
p->next=c;
c->next=d;
c->next=NULL;
return head;
}
}
}
}
LinkList*Delete(LinkList*head,int deleteNum){
LinkList*p,*q;
p=head;//////////////////////////////////////////////////////////////////////////对此处不确定
if(p==NULL){
return NULL;
}
else{
if(p->data==deleteNum){
head=head->next;
free(p);
return head;
}
else{
while(p!=NULL){
q=p->next;
if(q->data==deleteNum){
p->next=q->next;
free(q);
break;
}
p=p->next;
}
return head;
}
}
}
int Count(LinkList*head){
LinkList*p;
int length=0;
p=head->next;
while(p!=NULL){
length++;
p=p->next;
}
return length;
}
LinkList*Reverse(LinkList*head){
LinkList*p,*q;
p=head->next;
/*while(p!=NULL){
q=p->next;
p->next=q->next;
q->next=head->next;
head->next=q;
}
*/
head->next=NULL;
while(p){
q=p->next;
p->next=head->next;
head->next=p;
p=q;
}
return head;
}
int main()
{
LinkList* head=(LinkList*)malloc(sizeof(LinkList));
int length;
int order;
int deleteNum;
int x;
char c;
int insertnum;
int ListLength;
int position;
bool inputFlag;
inputFlag=true;
while(inputFlag)
{
printf("Please input number order:Create/0,Display/1,Sort/2,Search/3, Insert/4,Delete/5,Count/6,Revers/7\n");
scanf("%d",&order);
switch(order)
{
case 0:
printf("Creat the List\n");
printf("Please input the length:\n");
scanf("%d",&length);
head=Creat(length);
printf("The LIST is created!\n");
break;
case 1:
printf("Display the List:\n");
Display(head);
break;
case 2:
printf("Sort\n");
head=Sort(head);
break;
case 3:
printf("Search\n");
printf("Please input the number you want to search:\n");
scanf("%d",&x);
position =Search(head,x);
printf("The position of it is:%d\n",position);
break;
case 4:
printf("Insert\n");
printf("Please input the number you want to insert:\n");
scanf("%d",&insertnum);
head=Insert(head,insertnum);
break;
case 5:
printf("Delete\n");
printf("Please input the position of the number you want to delete:\n");
scanf("%d",&deleteNum);
head=Delete(head,deleteNum);
break;
case 6:
printf("Count\n");
ListLength=Count(head);
printf("%d\n",ListLength);
break;
case 7:
printf("Reverse\n");
head=Reverse(head);
break;
default:
break;
}
printf("Would you want to go on? Yes/Y or No/N\n");
scanf(" %c",&c);
if(c==‘Y‘)
{
inputFlag=true;
}
else if(c==‘N‘)
{
inputFlag=false;
}
else
{
printf("You input the wrong information!\n");
inputFlag=false;
}
}
return 0;
}