The program is done in vs2013!!!
Basic functions of a singly linked list:
#include<stdio.h> #include<malloc.h> typedef char ElemType; typedef struct LNode { ElemType data; struct LNode *next; } LinkList; void InitList(LinkList *&L) { L = (LinkList*)malloc(sizeof(LinkList)); L->next = NULL; } void DestoryList(LinkList *&L) { LinkList *p = L, *q = p->next; while (q != NULL) { free(p); p = q q = p->next; } free(p); } int ListEmpty(LinkList *L) { return(L->next == NULL); } int ListLength(LinkList *L) { LinkList *p = L; int i = 0; while (p->next != NULL) { i++; p = p->next; } return (i); } void DispList(LinkList *L) { LinkList *p = L->next; while (p != NULL) { printf("%c", p->data); p = p->next; } printf("\n"); } int GetElem(LinkList *L, int i, ElemType &e) { int j = 0; LinkList*p = L; while (j < i&&p != NULL) { j++; p = p->next; } if (p == NULL) return 0; else { e = p->data; return 1; } } int LocateElem(LinkList *L, ElemType e) { LinkList *p = L->next; int n = 1; while (p!= NULL&&p->data != e) { p = p->next; n++; } if (p == NULL) return (0); else return(n); } int ListInsert(LinkList *&L, int i, ElemType e) { int j = 0; LinkList *p = L, *s; while (j < i - 1 && p != NULL) { j++; p = p->next; } if (p == NULL) return 0; else { s = (LinkList*)malloc(sizeof(LinkList)); s->data = e; s->next = p->next; p->next = s; return 1; } } int ListDelete(LinkList *&L, int i, ElemType e) { int j = 0; LinkList *p = L, *q; while (j < i - 1 && p != NULL) { j++; p = p->next; } if (p == NULL) return 0; else { q = p->next; p->next = q->next; free(q); return 1; } }
//Create a new main.cpp under the same project and call the above function: the code is as follows
#include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef char ElemType; typedef struct LNode { ElemType data; struct LNode *next; } LinkList; extern void InitList(LinkList *&L); extern void DestoryList(LinkList *&L); extern int ListEmpty(LinkList *L); extern int ListLength(LinkList *L); external void DispList (LinkList * L); extern int GetElem(LinkList *L, int i, ElemType &e); extern int LocateElem(LinkList *L, ElemType e); extern int ListInsert(LinkList *&L, int i, ElemType e); extern int ListDelete(LinkList *&L, int i, ElemType e); intmain() { LinkList *h; ElemType e; printf("(1) Initialize singly linked list h\n"); InitList(h); printf("(2) Insert a,b,c,d,e elements by tail insertion method in turn\n"); ListInsert(h, 1, 'a'); ListInsert(h, 2, 'b'); ListInsert(h, 3, 'c'); ListInsert(h, 4, 'd'); ListInsert(h, 5, 'e'); printf("(3) output singly linked list h:"); DispList (h); printf("(4) The length of the singly linked list h is %d\n", ListLength(h)); printf("(5) singly linked list h is %s\n", (ListEmpty(h) ? "empty" : "not empty")); GetElem(h, 3, e); printf("(6)The third element of the singly linked list h=%c\n",e); printf("(7) The position of element a=%d\n",LocateElem(h,'a')); printf("(8)Insert f element at the position of the fourth element\n"); ListInsert(h,4,'f'); printf("(9) output singly linked list h:"); DispList (h); printf("(10) delete the third element of h\n"); ListDelete (x, 3, e); printf("(11) output singly linked list h:"); DispList (h); printf("(12) release the singly linked list h\n"); DestoryList(h); system("pause"); return 0; }