【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
- 【C--step by step①】链表
- RDL(C) Report Design Step by Step
- Step-by-step learning C + +(chapter fivesummary____Expressions)
- Step By Step(Lua调用C函数)
- Step By Step(Lua调用C函数)
- Step By Step(Lua调用C函数)
- Step By Step(Lua调用C函数)
- Step By Step(Lua调用C函数)
- Step By Step(C调用Lua)
- Step By Step(Lua-C API简介)
- Step By Step(Lua调用C函数)
- Step By Step(Lua调用C函数)
- Step By Step(Lua-C API简介)
- Step By Step(Lua调用C函数)
- 【C-step by step③】-二叉树
- NHibernate Step by Step
- OGRE step by step
- step by step
- Hadoop2.6.0 Snappy 压缩安装与配置
- 高并发 php uniqid 不重复唯一标识符生成方案
- Fragment中adapter刷新无效的解决办法
- android实现socket端口通信demo
- Android FlexboxLayout的简单了解以及使用
- 【C--step by step①】链表
- Linux C++程序解决文件句柄(包括socket句柄等)耗尽问题
- 刘一天@认真是一种态度 使劲的浪费,用力的后悔。 java监测方法运行时间/效率方法
- 有向图强连通分量 Tarjan算法
- 【bzoj2500】【幸福的道路】【树形dp+单调队列】
- Javascript模块化编程(二):AMD规范
- android framwork 分析之binder
- 最常用的Eclipse快捷键
- linux shell中怎样批量修改文件名为 文件夹_文件名