【C--step by step①】链表

来源:互联网 发布:烟台大学官网网络教学 编辑:程序博客网 时间:2024/06/05 19:24

声明:

仅作学习及练习使用。

一言不合,直接上代码!

Demo1.

------------------------------------------------------------------------

List.h

#ifndef LIST_H#define LIST_H#define BOOL int#define TRUE 0#define FALSE 1typedef int ElemType;struct LNode{    ElemType data;    struct LNode *next;} *head,*pthis;/*末尾插入新节点*/BOOL insert();/*删除指定位置节点*/BOOL delNode();/*末尾添加节点*/BOOL append();/*查找*/void search(ElemType e);/*打印*/void display(struct LNode *ptr);/*打印列表*/void list();#endif


List.c

#include <stdio.h>#include <stdlib.h>#include "List.h"BOOL insert(){    printf("\n\nPlease Inuput List data,Exit with -1:\n");    struct LNode *newNode;        do{        newNode = (struct LNode *)malloc(sizeof(struct LNode));        if(newNode == NULL)        {            printf("malloc new node failed!!\n");            return FALSE;        }        scanf("%d", &newNode->data);        newNode->next = NULL;                if(newNode->data != -1)        {            if(head == NULL)            {                head = newNode;                pthis = head;            }else{                pthis->next = newNode;                pthis = pthis->next;            }        }                    }while(newNode->data != -1);    free(newNode);        printf("exit create List.......");        return TRUE;}BOOL delNode(){    int value;    struct LNode *q;        if(head == NULL)    {        printf("\n\nNo data, Cannot delete!\n");        return FALSE;    }        printf("\n\nPlease Input your delete data value:\n");    scanf("%d", &value);        pthis = head;        if(value == head->data)    {                head = pthis->next;        free(pthis);            }else{        while(pthis->next && pthis->next->data != value)        {            pthis = pthis->next;            if(pthis->next == NULL)            {                printf("node not exists, Exit!\n");                return FALSE;            }        }                /*pthis 为待删除的前驱节点*/        q = pthis->next;        pthis->next = q->next;        free(q);    }        printf("delete success!");            return TRUE;}BOOL append(){    struct LNode *newNode;    printf("\n\nPlease Input new node data info:\n");    newNode = (struct LNode *)malloc(sizeof(struct LNode));    if(newNode == NULL)    {        printf("\nError!Can not apply for the required memory!!\n");        return FALSE;    }    scanf("%d", &newNode->data);    newNode->next = NULL;        if(head == NULL)    {        head = newNode;    }else{        pthis = head;        while(pthis->next != NULL)        {            pthis = pthis->next;        }        pthis->next = newNode;            }    //    free(newNode);        printf("append node success!\n\n");        return TRUE;}void search(ElemType e){    if(head == NULL)    {        printf("head is null, exit!");        exit(-1);    }        pthis = head;        while(pthis->next != NULL)    {        if(e == pthis->data)        {            display(pthis);            return;        }                pthis = pthis->next;    }        printf("NOT Find Node!!!");}void display(struct LNode *ptr){    if(ptr== NULL)    {        printf("point ptr is null,exit!");        exit(-1);    }    ElemType data = ptr->data;    printf("the date is-->%d .\n", data);}void list(){    if(head == NULL)    {        printf("List is null, exit.");        exit(-1);    }        pthis = head;    while(pthis != NULL)    {        display(pthis);        pthis = pthis->next;    }}

Main.c

#include <stdio.h>#include <stdlib.h>#include <conio.h>#include "list.h"int main(int argc, char **argv){char command = 0;    int data;    do{        printf("\n\n\t menu\n");        printf("-------------------------------\n");        printf("\tc,create List\n");        printf("\ts,query record\n");        printf("\tl,data List\n");        printf("\ta,append data\n");        printf("\td,del record\n");        printf("\te,exit system\n");                printf("-------------------------------\n");        printf("\tplease Select:");                command = getche();        printf("\n\n\n");                switch(command){            case 'c':                if(head == NULL)                {                    insert();                }else{                    printf("List already exists!\n");                }                break;            case 's':                printf("\n\nyou query data:");                scanf("%d", &data);                search(data);                break;            case 'l':                list();                break;            case 'a':                append();                break;            case 'd':                delNode();                break;            case 'e':                printf("Exit System=========\n\n");                break;            default:                printf("you press error key! try again...");                break;        }            }while(command != 'e');        printf("===========Game Over===========\n\n");        return 0;}


另外一种方式:

Demo2.

------------------------------------------------------------------------

#ifndef LIST_H#define LIST_H#define BOOL int#define TRUE 0#define FALSE 1typedef int ElemType;typedef struct{    ElemType data;    struct LNode *next;}LNode,*head,*pthis;/*末尾插入新节点*/BOOL insert(LNode *headPtr, ElemType e);/*删除指定位置节点*/BOOL delNode(LNode *head, int index);/*查找*/void search(LNode *head, ElemType e);/*打印*/void display(LNode *pthis);/*打印列表*/void list(LNode *head);#endif


0 0
原创粉丝点击