c链表

本文介绍了使用C语言创建、添加、查找、反转和删除链表节点的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>

struct node
{
int val;
node* next;
};
typedef node* list_node;
list_node create(int n )
{
list_node head , temp ,pre;
head = new node;
pre = head;
while ( n -- )
{
temp = new node;
scanf("%d" , &temp->val);
pre->next = temp;
pre = temp;
}
pre->next = NULL;
return head;
}
int add(list_node &head,int pos,int value)
{
list_node temp = head;
int count = 0 ;
while ( temp->next != NULL)
{
temp =temp->next;
count ++;

if (count == pos )
break;
}
if (temp->next == NULL)
return -1;
else
{
list_node n = new node;
n->val = value;
n->next = temp->next ;
temp->next = n ;
return 1;
}
}
int cont(const list_node head)
{
list_node temp = head;
int count = 0 ;
while ( temp ->next != NULL)
{
temp = temp->next;
count ++;
}
return count;
}
node* reverse( list_node temp , list_node &head )
{
if ( temp == NULL || temp->next == NULL)
{
head->next = NULL;
head = temp;
return head;
}
list_node tmp = reverse( temp->next , head);
tmp->next = temp;
return temp;
}

int del(list_node &head , int pos)
{
int count = 0 ;
list_node temp = head;
while ( temp->next != NULL)
{
temp = temp->next ;
count ++;
if ( count == pos - 1 )
break;
}
if ( temp->next == NULL )
{
return -1;
}
else
{
if ( temp->next->next == NULL)
temp->next = NULL;
else
temp->next = temp->next->next;
return 1;
}
}
int main()
{
list_node head = create(5);

del(head,3);

reverse(head,head);
return 0 ;
}
posted on 2012-03-30 00:00 lzhenf 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lzhenf/archive/2012/03/30/2424449.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值