/
created: 2009/09/15
created: 16:9:2009 17:20
filename: E:\dd\lianbiao\lianbiaocpp
author:
purpose: 一个win32 的控制台程序
实现单项链表的数据删除、插入、排序等功能
/
#include <stdioh>
#include "windowsh"
#include "malloch"
#define LEN sizeof(struct student)
#define NULL 0
#define N 5 //N为所要创建的链表的长度
int n = 0; //定义全局变量n,表示链表的节点个数
/
Author/data: /2009/09/15
Description: 声明一个结构体作为链表的节点
tip: student 只是一个标签,是不能声明变量的
/
struct student
{
int num;
float cj;
struct student Pnext;
};
/
Author/data: /2009/09/15
Description: 创建一个动态链表
参数: 返回链表的第一个节点的地址
x 为链表的长度
/
struct student pa(int x)
{
struct student head;
struct student p1,p2;
// n = 0;
p1 = p2 = (struct student )malloc(LEN); //开辟一个结构体内存
fflush(stdin); // 清除缓冲区数据 避免直接读入缓冲区数据
scanf("%d,%f",&p1->num,&p1->cj);
head = NULL;
while(p1->Pnext != NULL)
{
n = n+1;
if(n == 1)
{
head = p1; // 链表的头地址
}
else
{
p2->Pnext = p1; //链接链表数据
}
/ 判断是否最后一个 /
if(n >= x)
{
p1->Pnext = NULL;
}
else
{
p2 = p1;
p1 = (struct student )malloc(LEN);
fflush(stdin);
scanf("%d,%f",&p1->num,&p1->cj);
}
}
return(head);
}
/
Author/data: /2009/09/15
Description: 输出一个链表
参数: head为第一个节点的地址
/
void print(struct student head)
{
struct student p;
int i = 0;
printf("\nNow,These %d records are:\n",n);
p = head;
if(head != NULL) // 如果链表不是空的,则进行数据输出
{
do
{
printf("%d\t",i++);
printf("%5d %51f\n",p->num,p->cj); // 输出链表数据
p = p->Pnext;
}while(p != NULL);
}
}
/
Author/data: /2009/09/16
Description: 释放动态链表的地址空间
参数: 链表的头地址head
无返回值
/
void freelinkspace(struct student head)
{
struct student Ltemp;
LtempPnext = head->Pnext; // Ltemp 用来存放->next,避免空间被
// 释放后,找不到下一个结点的地址
while(head->Pnext != NULL) // 判断是否已经空间释放到最后一个
{
free(head);
head = LtempPnext;
LtempPnext = head->Pnext;
}
free(head); // 释放最后一个结点空间
}
/
Author/data: /2009/09/15
Description: 删除链表链表中的一个结点
参数: head 为第一个节点的地址
num 为要删除结点的序号(head->num)
/
struct student del(struct student head,int num)
{
struct student p1,p2;
p1 = head;
while(p1->num!=num && p1->Pnext!=NULL) // 寻找要删除的结点
{
p2 = p1; // p2 存放删除结点的前一个结点地址
p1 = p1->Pnext; // p1 存放要删除的结点
}
if(num == p1->num) // 是否找到要删除结点
{
if(p1 == head) // 删除的是第一个结点
{
head = p1->Pnext;
}
else if(p1->Pnext == NULL) // 删除的是最后一个结点
{
p2->Pnext = NULL;
}
else // 删除中间的结点
{
p2->Pnext = p1->Pnext;
p1->Pnext = NULL;
}
printf("delete: %d\n",num);
n = n-1; // 链表长度 - 1
}
else
{
printf("%d not been found! \n",num);
}
delete(p1);
return(head);
}
/
Author/data: /2009/09/16
Description: 添加一个结点到链表中
参数: head 为第一个结点的地址指针
stud 为要插入的结点的地址指针
/
struct student insert(struct student head,struct student stud)
{
struct student p0,p1,p2;
p0 = stud;
p1 = head;
while(p0->num>p1->num && p1->Pnext!=NULL) // 找到添加结点的位置
{
p2 = p1; // p2 存放要添加的前一个结点的地址
p1 = p1->Pnext; // p1 存放要添加的后一个结点的地址
}
if(p0->num<=p1->num && p1->Pnext!=NULL)
{
if(p1 == head) // 添加结点到第一个位置
{
p0->Pnext = p1;
head = p0;
}
else
{
p2->Pnext = p0;
p0->Pnext = p1;
}
}
else // 添加结点到最后一个位置
{
p1->Pnext = p0;
p0->Pnext = NULL;
}
n = n+1; // 结点数目 + 1
return(head);
}
/
Author/data: /2009/09/16
Description: 链表的重新排序===按照num(不能重复)的大小从小到大排
列链表数据
参数: head 接收链表第一个节点的地址指针
返回值为新生成链表的第一个节点的地址指针
/
struct student linkpaix(struct student head)
{
struct student stemp,ltemp,shead,head_h; / /
struct student p,q; / 申请两个链表指针,用来储存链表交换过
程的中间值 /
head_h = head;
ltemp = head;
p = (struct student ) malloc(LEN);
q = (struct student ) malloc(LEN); / 为p,q开辟动态存储空间 /
/ -==== 先将链表的第一个数据与其他数据比较 ====- /
while(head->Pnext != NULL)
{
shead = head;
head = head->Pnext;
if(ltemp->num > head->num)
{
if(ltemp == shead)
{
ltemp ->Pnext = head ->Pnext;
head ->Pnext = ltemp;
}
else
{
p->Pnext = head ->Pnext;
q->Pnext = ltemp ->Pnext;
head ->Pnext = q->Pnext;
shead ->Pnext = ltemp;
ltemp ->Pnext = p->Pnext;
}
head_h = head;
head = ltemp;
ltemp = head_h;
}
}
/ -==== 先将链表的第一个数据与其他数据比较 ====- /
/ -==== 比较链表第一个以外的数据 ====- /
while(ltemp ->Pnext != NULL)
{
stemp = ltemp;
ltemp = ltemp ->Pnext;
head = ltemp;
while(head->Pnext != NULL)
{
shead = head;
head = head->Pnext;
if(ltemp->num > head->num)
{
if(ltemp == shead)
{
p->Pnext = head ->Pnext;
stemp ->Pnext = head;
head ->Pnext = ltemp;
ltemp ->Pnext = p->Pnext;
}
else
{
p->Pnext = head ->Pnext;
q->Pnext = ltemp ->Pnext;
stemp ->Pnext = head;
head ->Pnext = q->Pnext;
shead ->Pnext = ltemp;
ltemp ->Pnext = p->Pnext;
}
head = ltemp;
ltemp = stemp ->Pnext;
}
}
}
/ -==== 比较链表第一个以外的数据 ====- /
free(p);
free(q); // 释放p,q的动态存储空间
return(head_h);
}
/
Author/data: /2009/09/15
Description: 主函数
参数:
/
void main()
{
struct student phead,pins; // 定义2个链表指针
int delnum, selflog,flog_a = 1,Nf = 1; // 要删除的对象id
char delflog ; // 删除标志y/n
char insflog, flog_nx = 'n';
char flog_free ; // 释放标志
/ === 输入N个数据 === N 为定值
printf("please input %d numbers:\n",N);
phead = pa(N); // 创建一个动态链表,并赋值
print(phead); // 输出链表数据
/
/ === 输入Nx个数据 === Nx 为输入值 === /
int Nx; // Nx 想要输入的链表长度
printf("How long do you want establish \t");
fflush(stdin);
scanf("%d",&Nx);
/ -== 判断创建的是否是一个空链表 ==- /
while(Nx == 0)
{
printf("您要创建的是一个空链表,是否确定?y/n \t");
fflush(stdin);
scanf("%c",&flog_nx);
if(flog_nx == 'n')
{
printf("How long do you want input\t");
fflush(stdin);
scanf("%d",&Nx);
}
else if(flog_nx == 'y') goto endl;
else
{
printf("wrong input!\n");
printf("How long do you want input\t");
fflush(stdin);
scanf("%d",&Nx);
}
}
printf("please input %d numbers: ",Nx);
printf("如:1,3 两个数中间以,隔开\n");
phead = pa(Nx); // 创建一个动态链表,并赋值
print(phead); // 输出链表数据
/ -== 链表 *** 作 ==- /
while(flog_a)
{
if(phead == NULL) {printf("链表已空,无法 *** 作\n"); flog_a = 0; break;}
printf("\n *** 作\n1:\t插入数据\n2:\t删除数据\n3:\t排序\n4:\t清屏\n5:\t输出现在的链表数据\n0:\texit\n");
printf("\nPlease input:\t");
fflush(stdin);
if(scanf("%d",&selflog)) // select flog 选择项
switch(selflog)
{
case 1 :
/ ====== 插入数据到链表 ====== /
printf("insert someone y/n\t");
fflush(stdin);
scanf("%c",&insflog);
while(insflog != 'n')
{
while(insflog != 'y'&& insflog != 'n')
{
printf("wrnong input,please input again \n");
printf("another one y/n\t");
fflush(stdin);
scanf("%c",&insflog);
}
printf("please input the date:\n");
pins = (struct student )malloc(LEN);
fflush(stdin);
scanf("%d,%f",&pins->num,&pins->cj);
phead = insert(phead,pins);
print(phead);
printf("another one y/n\t");
fflush(stdin);
scanf("%c",&insflog);
while(insflog != 'y'&& insflog != 'n')
{
printf("wrnong input,please input again \n");
printf("another one y/n\t");
fflush(stdin);
scanf("%c",&insflog);
}
}
/ ====== 插入数据到链表 ====== /
break;
case 2 :
/ ====== 删除链表中的数据 ====== /
printf("del someone y/n\t");
fflush(stdin);
scanf("%c",&delflog);
while(delflog != 'n' && phead != NULL)
{
while(delflog != 'y'&& delflog != 'n')
{
printf("wrnong input,please input again \n");
printf("del someone y/n\t");
fflush(stdin);
scanf("%c",&delflog);
}
printf("please input the student what you want del:\n");
fflush(stdin);
scanf("%d",&delnum);
phead = del(phead,delnum);
print(phead);
printf("another one y/n\t");
fflush(stdin);
scanf("%c",&delflog);
if((n+1)==0)
{
printf("There is no more num could be del!\n");
break;
}
}
/ ====== 删除链表中的数据 ====== /
break;
case 3 :
/ ====== 排列链表数据 ====== /
printf("\n排序之后:");
phead = linkpaix(phead);
print(phead); // 排序该数据链表
/ ====== 排列链表数据 ====== /
break;
case 4 :// clrscr();
system("cls");
break;
case 5 : print(phead); break;
case 0 : flog_a = 0 ; break; / 退出 /
default : printf("wrong input\nPlease input again");
break;
}
else printf("非法输入!\n");
}
endl: while(1)
{
if(Nx == 0)
{
printf("Can't establish the link!\n");
break;
}
printf("\n保留数据?y/n\t"); // 是否释放地址空间
fflush(stdin);
scanf("%c",&flog_free);
if(flog_free == 'y')
{
break;
}
else if(flog_free == 'n')
{
freelinkspace(phead);
break;
}
else
{
printf("wrong input!\n");
}
}
printf("OVER!\nGOOD LUCK!\n");
}1安装grails
下载grails,然后配置:
GRAILS_HOME=”D:/JavaDev/grails”
path追加:%GRAILS_HOME%/bin
2创建grails项目
3配置数据源
打开conf/DataSourcegroovy
dataSource {
dbCreate = "update"
url = "jdbc:mysql://1921685811:3306/grailsdbuseUnicode=true&characterEncoding=UTF-8"
driverClassName = "commysqljdbcDriver"
username = "root"
password = "root"
}
hibernate {
cacheuse_second_level_cache = true
cacheuse_query_cache = false
cacheregionfactory_class = 'netsfehcachehibernateEhCacheRegionFactory' // Hibernate 3
// cacheregionfactory_class = 'orghibernatecacheehcacheEhCacheRegionFactory' // Hibernate 4
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://1921685811:3306/grailsdbuseUnicode=true&characterEncoding=UTF-8"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://1921685811:3306/grailsdbuseUnicode=true&characterEncoding=UTF-8"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://1921685811:3306/grailsdbuseUnicode=true&characterEncoding=UTF-8"
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=false
validationQuery="SELECT 1"
jdbcInterceptors="ConnectionState"
}
}
}
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546
4引入数据库驱动包
lib下添加mysql-connector-java-5132jar
用命令refresh-dependencies添加到依赖
5新建Domain类。
两种方式:(1)通过命令:create-domain-class Book(2)右键-〉New-〉Domain Class
Book代码如下:
package hellograils
class Book {
String title
String content
static constraints = {
}
}12345678
6创建controller和views:两种方式
(1)打Book类,点击图标上面的Controller 和Views 就可以产生代码了
(2)命令grails generate-all hellograilsBook
grails会自动创建Controller(会自动生成增、删、改、查的相关方法)和View(会自动生成增、删、改、查的相关页面)mvc有自带的增删查改,在models写好类在contrller右键,添加控制器,选择“带有读写功能的视图”,基类就选你刚建的类就行了,上下文类就自己编写个继承DbContext的类就行了,在里面加个属性,dbset<刚建的 类> 创建好后,其他全部自动生成了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)