/* This is a framework for the implementation of a linked list. */
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <malloc.h>
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2
#define LIST_INIT_SIZE 100 #define LISTINCREMENT 10
typedef int Status; typedef int ElemType;
typedef struct LNode{ ElemType data; struct LNode *next; }LNode, *LinkList;
LinkList L1;
Status DestroyList_L(LinkList *L); LinkList CreatList_L(LinkList L,int n); LNode * GetElem_L(LinkList L,int i); Status DispList_L(LinkList L); Status ListInsert_L(LinkList L,int i, ElemType e); Status ListDelete_L(LinkList L,int i);
void main() { char c; int e,d,n;
while(1){
printf("List Implementation by Sequence\n");
printf("===========================================\n");
printf("1. Create a list; 2. Display a list; \n");
printf("3. Insert an element; 4.Delete an element; \n");
printf("5. Get an element; 6. Destroy a list\n");
printf("0. Exit;\n");
printf("===========================================\n");
scanf(" %c",&c);
switch(c){
case '1':
printf("Input the number of nodes:\n");
scanf(" %d",&n);
L1 = CreatList_L(L1,n);
break;
case '2':
DispList_L(L1);
break; case '3':
printf("Input a location and an element:\n");
scanf("%d%d",&d,&e);
if(ListInsert_L(L1,d,e))
printf("insert successfully!");
else
printf("insert failure!");
break;
case '4':
printf("Input a location:\n");
scanf(" %d",&d);
ListDelete_L(L1,d);
break;
case '5':
printf("Input a location to get an element:\n");
scanf(" %d",&d);
if(!GetElem_L(L1,d))
printf("Location Error!\n");
else
printf("The element is %d\n",GetElem_L(L1,d)->data);
break;
case '6':
DestroyList_L(&L1);
break;
case '0':exit(0);
default:
printf("Print an incorrect letter;\n");
break;
}
}
system("PAUSE");
}
Status DestroyList_L(LinkList *L)//there is a problem { LNode *p,*q; p = *L;
if (p!=NULL)//one node
{
q = p->next;
if (q==NULL)
free(p);
else
while(q)
{
free(p);
p = q;
q = q->next;
}
}
return OK;
}
LinkList CreatList_L(LinkList L,int n) { int i; LNode *p;
L = (LinkList )malloc(sizeof(LNode));
L->next = NULL;
for(i=n; i > 0 ; --i) {
p = (LinkList )malloc(sizeof(LNode));
printf("Please input the %d-th element:\n",n-i+1);
scanf("%d",&p->data);
p->next = L->next;
L->next = p;
}
printf("A linked list with %d elements has been created!\n", n);
return L;
}
LNode *GetElem_L(LinkList L,int i)//5 { int k=0; LNode *p; p=L->next ; if(i<0) return ERROR; while(k<i-1){ p=p->next ; if(p==NULL) return ERROR; k++; } return p;
}
Status DispList_L(LinkList L) { LNode *p=L; if (p==NULL) //if (p==NULL) { printf("A null list!"); return 0; } p=p->next; while (p!=NULL) { printf("%5d", p->data); p=p->next; } printf("\n"); return OK; }
Status ListInsert_L(LinkList L,int i, ElemType e)//e is the date
{ int k=0;
LNode*p,*s;
p=L->next ;//location
if(i<0) return ERROR;
while(k<i-1){
p=p->next ;
if(p==NULL) return ERROR;
k++;
} s=(LinkList )malloc(sizeof(LNode)); s->data=e; s->next=p->next; p->next=s;
}
Status ListDelete_L(LinkList L,int i) {int k=0; LNode*p,*s; p=L->next; if(i<0) return ERROR; while(k<i-2){ p=p->next ; if(p==NULL) return ERROR; k++; } s=p->next ; p->next=s->next; free(s);
}