07单链表的一些基本操作(创建、测量、插入、打印、删除)

结果如下图:

单链表的一些基本操作(创建、测量、插入、打印、删除) - 458905216 - SaEe的博客

代码如下:

#include <stdio.h>
#include <malloc.h>


/*1********************************************************************/
/*链表节点的定义*/
typedef struct node
{
int data; /*节点内容*/
struct node *next; /*下一个节点*/
}node;

/*单链表的创建*/
node *create()
{
int i = 0; /*链表中数据的个数*/
node *head , *p , *q;
int x = 0; /*存储数据*/
head = (node *)malloc(sizeof(node)); /*创建头节点*/

printf("input 0 to end!\n");
while(1)
{
printf("please input the data:");
scanf("%d" , &x);

if(x == 0) /*数据为0时表示插入数据结束*/
break;

p = (node *)malloc(sizeof(node)); /*创建一个新节点*/
p->data = x;
if(++i == 1) /*链表只有一个元素*/
{
head->next = p; /*链接到head后面*/
}
else
{
q->next = p; /*链接到链表尾端*/
}
q = p; /*q指向末节点*/
}
q->next = NULL; /*链表的最后一个指针为NULL*/
return head;
}


/*2********************************************************************/
/*单链表的测长*/
int length(node *head) /*返回单链表长度*/
{
int len = 0;
node *p; /*新建一个节点指针*/
p = head->next; /*指针指向头节点的下一个节点*/
while(p != NULL) /*遍历链表*/
{
len++;
p = p->next;
}
return len;
}
/*由于链表末尾节点的next指针被置为NULL,
因此可以使用while循环进行遍历链表所有节点,
当遇到NULL时结束循环*/


/*3********************************************************************/
/*打印单链表*/
void print(node *head)
{
node *p; /*新定义一个节点指针*/
int index = 0; /*记录元素号*/
if(head->next == NULL) /*链表为空*/
{
printf("Link is empty!\n");
return ;
}

p = head->next; /*节点指针指向下一个节点*/
while(p != NULL) /*遍历链表*/
{
printf("The %dth node is:%d\n" , ++index , p->data);/*打印元素*/
p = p->next;
}
}
/*单链表的打印与单链表的测长方法类似,使用while循环
进行遍历链表所有节点并打印各个节点内容,当遇NULL结束*/

/*4********************************************************************/
/*查找单链表pos位置的节点,返回节点指针*/
/*pos从0开始,0返回head节点*/
node *search_node(node *head , int pos)
{
node *p = head->next;/*新建节点指针并指向头节点的下个节点*/

if(pos < 0) /*pos位置不正确*/
{
printf("incorrect position to search node!\n");
return NULL;
}
if(pos == 0) /*在head位置,返回head*/
{
return head;
}

if(p == NULL)
{
printf("Link is empty!\n"); /*链表为空*/
return NULL;
}

while(--pos)
{
if((p = p->next) == NULL)
{
printf("incorrrect position to search node !\n");
break; /*超出链表返回*/
}
}
return p;
}

/*5********************************************************************/
/*单链表pos位置处插入节点,返回链表头指针*/
/*pos从0开始计算,0表示插入到head节点后面*/
node *insert_node(node *head , int pos , int data)
{
node *item = NULL;
node *p;
item = (node *)malloc(sizeof(node)); /*新建插入的节点*/
item->data = data; /*给新节点赋值*/

if(pos == 0) /*插入链表头后面*/
{
head->next = item; /*head后面是item*/
return head;
}

p = search_node(head , pos); /*获得位置pos的节点指针*/
if(p != NULL)
{
item->next = p->next; /*item指向原pos节点的后一个节点*/
p->next = item; /*把item插入到pos的后面*/
}
return head;
}

/*6********************************************************************/
/*删除单链表的pos位置的节点,返回链表头指针*/
/*pos从1开始计算,1表示删除head后的第一个节点*/
node *delete_node(node *head , int pos)
{
node *item = NULL;
node *p = head->next;

if(p == NULL)
{
printf("Link is empty!\n"); /*链表为空*/
return NULL;
}

p = search_node(head , pos-1); /*获得位置pos的节点指针*/
if(p != NULL && p->next != NULL)
{
item = p->next;
p->next = item->next;
//delete item;
free(item);
}
return head;
}

/*7********************************************************************/

int main()
{

node *head = create(); /*创建单链表*/
printf("\n**************************************\n");
printf("Length:%d\n" , length(head)); /*测量单链表长度*/
head = insert_node(head , 2 , 5); /*在第2个节点之后插入5*/
printf("\n**************************************\n");
printf("insert integet 5 after 2th node:\n");
print(head); /*打印单链表*/
head = delete_node(head , 2); /*删除第2个节点*/
printf("\n**************************************\n");
printf("delete the 2th node:\n");
print(head);
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值