数据结构(c语言版) 在线性表中删除一个节点的简单c程序,数据结构(C语言版)中的删除链表中的一个节点...

简单写了个 你试哈?

#include

#include

struct node

{

int data;

node *next;

};

struct node *head = null;

struct node *tail = null;

void createlist();

void displaynode();

void delnode(int node);

node *searchnode(int node);

int main()

{

createlist();

displaynode();

delnode(1);//删除第三个结点

displaynode();

getchar();

getchar();

return 0;

}

void createlist()

{

int num;

printf("请输入结点数:");

scanf("%d",&num);

for( int i = 0; i < num; i++ )

{

struct node *test = (node *)malloc(sizeof(node));

printf("请输入第%d结点数据:",i+1);

scanf("%d",&test->data);

if ( head == null )

{

head = test;

test->next = null;

tail =test;

}

else

{

tail->next = test;

test->next = null;

tail = test;

}

}

}

void displaynode()

{

node *ptemp = head;

while(ptemp)

{

printf("%d ",ptemp->data);

ptemp = ptemp->next;

}

printf("\n");

}

void delnode(int node)

{

node *cur = searchnode(node-1);

if( cur == head )

head = cur->next;

else

{

node  *pre = searchnode(node-2);

pre->next = cur->next;

}

}

node *searchnode(int node)

{

node *phead = head;

int nnum = 0;

while ( phead )

{

if( nnum == node )

break;

phead = phead->next;

nnum++;

}

return phead;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值