C语言怎么编写一个删除的程序?

C语言怎么编写一个删除的程序?,第1张

#include "stdio.h" \x0d\x0a\x0d\x0a/*定义学生结构体*/ \x0d\x0a\x0d\x0astruct Student \x0d\x0a\x0d\x0a{ \x0d\x0a\x0d\x0achar ID[20]\x0d\x0a\x0d\x0achar Name[20]\x0d\x0a\x0d\x0afloat Mark1\x0d\x0a\x0d\x0afloat Mark2\x0d\x0a\x0d\x0afloat Mark3\x0d\x0a\x0d\x0afloat Average\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a/*声明学生数组及学生数量*/ \x0d\x0a\x0d\x0astruct Student students[1000]\x0d\x0a\x0d\x0aint num=0\x0d\x0a\x0d\x0a/*求平均值*/ \x0d\x0a\x0d\x0afloat Avg(struct Student stu) \x0d\x0a\x0d\x0a{ \x0d\x0a\x0d\x0areturn (stu.Mark1+stu.Mark2+stu.Mark3)/3\x0d\x0a\x0d\x0a} \x0d\x0a\x0d\x0a/*通过学号返回数组下标*/ \x0d\x0a\x0d\x0aint Student_SearchByIndex(char id[]) \x0d\x0a\x0d\x0a{ \x0d\x0a\x0d\x0aint i\x0d\x0a\x0d\x0afor (i=0i 回答于 2022-11-16

链表保存到文件,删除节点,添加节点。

#include

"stdio.h"

#include

"malloc.h"

#include

/*

*

*

用户数据结构

*/

typedef

struct

struData

{

char

usertext[100]

int

userdata1

int

userdata2

}ST_DATA,*PST_DATA

/*

*

*

链表结构

*/

typedef

struct

struLink

{

ST_DATA

userdata

struct

struLink*

pnext

}ST_LINK,*PST_LINK

/*

*

*

链表 *** 作

申请节点

*/

PST_LINK

MallocLinkNode()

{

PST_LINK

pLink

=

NULL

pLink

=(PST_LINK)

malloc(sizeof(ST_LINK))

if

(pLink)

{

memset(pLink,0,sizeof(ST_LINK))

}

return

pLink

}

/*

*

*

链表 *** 作

添加节点(末尾)

*/

void

AddNodeToLinTail(PST_LINK

pNodeHead,PST_LINK

pNodeAdd)

{

while(pNodeHead->pnext

!=

0)

{

pNodeHead

=

pNodeHead->pnext

}

pNodeHead->pnext

=

pNodeAdd

pNodeAdd->pnext

=

0

}

/*

*

*

链表 *** 作

添加节点(头部)

*/

PST_LINK

AddNodeToLinHead(PST_LINK

pNodeHead,PST_LINK

pNodeAdd)

{

pNodeAdd->pnext

=

pNodeHead

return

pNodeAdd

}

/*

*

*

链表 *** 作之

删除节点(假设

member

==

nCondition

为删除条件)

*

返回值为新的头部,因为有可能头部被删除。

*/

PST_LINK

DelLinkNode(PST_LINK

pNodeHead,int

nCondition)

{

PST_LINK

pTemp

=

pNodeHead

PST_LINK

pTemp2

/*在头部满足条件*/

if

(

pNodeHead->userdata.userdata1

==

nCondition)

{

pTemp

=

pNodeHead->pnext

free(pNodeHead)

/*头部被删除,返回下个节点当头部*/

return

pTemp

}

else

{

/*中间节点寻找满足的条件*/

while(pNodeHead->pnext

!=

0)

{

/*如果下一个节点满足条件*/

if

(pNodeHead->pnext->userdata.userdata1

==

nCondition)

/*这里是需要修改的地方*/

{

/*下一个满足了,我们删除下个节点,先保存它,以便释放内存*/

pTemp2

=

pNodeHead->pnext

/*把它后面的节点连上来,覆盖要删除的节点*/

pNodeHead->pnext

=

pNodeHead->pnext->pnext

/*释放删除节点内存*/

free(pTemp2)

/*这里可以返回了,要返回原先的链表头*/

return

pTemp

}

/*测试下一个*/

pNodeHead

=

pNodeHead->pnext

}

}

/*到这里,说明找不到,把原先的头部返回*/

return

pTemp

}

int

GetNodeCount(PST_LINK

pLinkHead)

{

int

nCount

=

0

while(pLinkHead)

{

nCount++

pLinkHead

=

pLinkHead->pnext

}

return

nCount

程序自删除的方式有很多,不过最终的思想不过是关闭本身进程,开启新进程用于删除自身。下面这个方法是用windows自带命令行程序实现删除。命令行为

cmd.exe

/c

del

filename--------------------------------------------------------------------------------代码示例:C语言形式#include

#include

#include

BOOL

SelfDel(){SHELLEXECUTEINFO

seiTCHAR

szModule

[MAX_PATH],szComspec[MAX_PATH],szParams

[MAX_PATH]//

获得自身文件名.

获取cmd的全路径文件名if((GetModuleFileName(0,szModule,MAX_PATH)!=0)

&&(GetShortPathName(szModule,szModule,MAX_PATH)!=0)

&&(GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0)){//

设置命令参数.lstrcpy(szParams,"/c

del

")lstrcat(szParams,

szModule)lstrcat(szParams,

"

>

nul")//

设置结构成员.sei.cbSize

=

sizeof(sei)sei.hwnd

=

0sei.lpVerb

=

"Open"sei.lpFile

=

szComspecsei.lpParameters

=

szParamssei.lpDirectory

=

0

sei.nShow

=

SW_HIDEsei.fMask

=

SEE_MASK_NOCLOSEPROCESS//

创建cmd进程.if(ShellExecuteEx(&sei)){//

设置cmd进程的执行级别为空闲执行,使本程序有足够的时间从内存中退出.SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS)//

将自身进程的优先级置高SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS)SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL)//

通知Windows资源浏览器,本程序文件已经被删除.SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,szModule,0)return

TRUE}}return

FALSE}---------------------------------------------------------------------------------代码示例:汇编形式.386.model

flat,stdcalloption

casemap:noneinclude

windows.incinclude

kernel32.incincludelib

kernel32.libinclude

urlmon.incincludelib

urlmon.libinclude

shell32.incincludelib

shell32.lib.data?szModule

db

MAX_PATH

dup

(?)sComspec

db

MAX_PATH

dup

(?)

自删除szParams

db

MAX_PATH

dup

(?)hCurProcess

dd

?hCurThread

dd

?.datasei

SHELLEXECUTEINFO

<0>.constszComspec

db

COMSPEC,0Verb1

db

open,0szCom1

db

/c

del

,0szCom2

db

>

nul,0.codestart:invoke

GetModuleFileName,0,addr

szModule,MAX_PATHinvoke

GetEnvironmentVariable,addr

szComspec,addr

sComspec,MAX_PATHinvoke

lstrcpy,addr

szParams,addr

szCom1invoke

lstrcat,addr

szParams,addr

szModuleinvoke

lstrcat,addr

szParams,addr

szCom2mov

sei.cbSize,sizeof

sei

1mov

sei.hwnd,0

2mov

sei.lpVerb,offset

Verb1

3lea

eax,szParamsmov

sei.lpParameters,eax

4lea

eax,sComspecmov

sei.lpFile,eax

5mov

sei.lpDirectory,0mov

sei.nShow,SW_HIDEmov

sei.fMask,SEE_MASK_NOCLOSEPROCESSinvoke

ShellExecuteEx,

addr

sei.if

eaxinvoke

SetPriorityClass,sei.hProcess,IDLE_PRIORITY_CLASSinvoke

GetCurrentProcessmov

hCurProcess,eaxinvoke

SetPriorityClass,hCurProcess,REALTIME_PRIORITY_CLASSinvoke

GetCurrentThreadmov

hCurThread,eaxinvoke

SetThreadPriority,hCurThread,THREAD_PRIORITY_TIME_CRITICALinvoke

SHChangeNotify,SHCNE_DELETE,SHCNF_PATH,addr

szModule,0&n


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11370919.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存