c链表

#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 ;
}
原文地址:https://www.cnblogs.com/lzhenf/p/2424449.html