单链表及双链表常用方法

单链表

存储结构定义

typedef struct Node 
{   
     int data; 
    struct Node *next; 
    
}Node, *ptr_Node;

typedef enum Status 
{ 
	SUCCESS ,
	ERROR 
}Status;

根据数组生成一条链表,成功则返回头节点,失败返回NULL

ptr_Node create(int *arr, int n){    
      ptr_Node head, p1 , p2;
   	  p1 = p2 = (Node *)malloc(sizeof(Node));
   	  if(p1 == NULL || p2 == NULL)
   	  return NULL;
   	  head = NULL;
	 for(int i = 0; i < n ;i++)
	 {   
	    p1->data = arr[i];
	   if(i==0)
	     head = p1;
	   else  
	     p2->next = p1;
	     p2 = p1;
        p1  = (Node*)malloc(sizeof(Node));  
     } 
      p2->next = NULL;
      free(p1);
      p1 = NULL;
     return head;
}

计算链表的长度

int  size(ptr_Node head){ 
    ptr_Node p = head;
         int i = 1;
       while(p != NULL && p->next != NULL){
             i++;
	         p = p->next;
       }
      return i;
}

销毁给定的链表

void destroy(ptr_Node head){   
   ptr_Node p = head ;
	  while (head != NULL) {  
        p = head->next;   
        free(head);  
        head = p;  
       }
}

在第 index 位后面插入 node 结点 
 插入成功返回 SUCCESS,失败返回 ERROR 

Status insert(ptr_Node *head, ptr_Node node, int index){   
    int  n = size(*head);
    if(*head == NULL || index < 0 || index > n){
     	return ERROR;
       }
     ptr_Node p = *head ;  
    if (*head == NULL) {  
             *head = node;  
        node->next = NULL;  
         return SUCCESS;
     	}
   else{  
        int i = 0;
	    while(i++ < index-1){
		   p = p->next;
     	 }
        node->next = p->next;   
           p->next = node; 
           return SUCCESS;
       } 
}

删除第 index 位节点后面的节点,将删除的结点的值保存到(*data)中 
删除成功返回 SUCCESS,其他情况返回 ERROR 

Status delete1(ptr_Node *head, int index, int *data){ 
      int n = size(*head);
     if( *head == NULL || index < 0 || index >= n)
       return ERROR;
         int i = 0;
    ptr_Node p = *head; 
    ptr_Node q = NULL;
	while(i++ < index-1){
 	      p = p->next;
      }
              q = p->next;
          *data = q->data;
        p->next = q->next ;
      free(q);
      q = NULL; 
     return SUCCESS;
}

在链表中查找节点值与 data 相等的节点,并返回找到的第一个节点的前一个节点的位置 
(例:头节点相等,返回 0),没找到或者其他情况返回-1

int search(ptr_Node head, int data){ 
    ptr_Node p = head;
     int     i = -1;
    while(p != NULL){ 
	     i++;
       if(p->data == data)
         return i;
       p = p->next;
      } 
     return -1;
}

将链表中 index 位点后面的结点的值修改为(*data),将原值保存到(*data) 
修改成功返回 SUCCESS,其他情况返回 ERROR 

Status edit(ptr_Node head, int index, int *data){    
   int n = size(head);
  if(index < 0 || index > n){
       return ERROR; 
    }
    ptr_Node p = head;
    int i = 0;
	while(i++ < index-1){
	   p = p->next;
    }
      int temp = p->data;
       p->data = *data;
      	 *data = temp;
      	 
     return SUCCESS;
}

 


 

双链表

存储结构定义

typedef struct TNode 
{ 
	int data; 
    struct TNode *pre;
	struct TNode *next;
}TNode, *ptr_TNode;

typedef enum Status 
{
	SUCCESS,
	ERROR 
}Status;

根据数组生成一条双向链表,成功则返回头结点,失败返回NULL

ptr_TNode create_T(int *arr, int n)
{
      ptr_TNode head,p1,p2;
   	  p1 = p2 = (TNode *)malloc(sizeof(TNode));
   	  if(p1 == NULL || p2 == NULL)
   	  return NULL;
   	  head = NULL;
	 for(int i = 0; i < n ;i++)
	 {   
	       p1->data = arr[i];
	   if( i == 0)
	    {
	           head = p1;
	     head->next = NULL;
	     head->pre  = NULL;
	             p2 = p1;
	     }
	     p1->next = NULL;
	     p1->pre  = p2;
	     p2->next = p1;
	      p2 = p1;
          p1 = (TNode*)malloc(sizeof(TNode));       
		   if( p1 == NULL )
   	         return NULL;	    
     } 
     return head;
}

计算链表的长度 

int  size_T(ptr_TNode head)
{ 
    ptr_TNode p = head;
         int i = 1;
       while(p != NULL && p->next != NULL){
        	 i++;
	        p = p->next;
       }
      return i;
	
}

双向链表,删除第 index 位节点后面的节点,将删除的结点的值保存到(*data)中 

Status delete_T(ptr_TNode *head, int index, int *data)
{
     int  n = size_T(*head);
     if( *head == NULL || index < 0 || index >= n)
       return ERROR;
    ptr_TNode p = *head;
        int   i = 0;
  	while(i++ < index){
	   p = p->next;
       }
           *data = p->data;
	p->pre->next = p->next;
	p->next->pre = p->pre;
	
}

双向链表,在第 index 位后面插入 node 结点 
插入成功返回 SUCCESS,失败返回 ERROR

Status insert_T(ptr_TNode *head, ptr_TNode node, int index)
{ 
        int   n = size_T(*head);
     if( *head == NULL || index < 0 || index > n){
       	printf("插入失败\n"); 
       return ERROR; 
       }
    ptr_TNode p1 = *head;
        int   i = 1;
  	while(i++ < index){
	   p1 = p1->next;
       }
   ptr_TNode p2 = p1->next;
	   p1->next = node;
   	 node->pre  = p1;
     node->next = p2;
	    p2->pre = node;
		if(p1->next->data == node->data && p1->next == node) {
	   printf("插入成功\n");
	    return SUCCESS;
     	} else {  
     	printf("插入失败\n");
     	return ERROR;
     	} 
}

销毁给定的链表 

void destroy_T(ptr_TNode head)
{    
     ptr_TNode p1 = head;
     ptr_TNode p2 = NULL;
     while (p1){   
	    p2 = p1;
        p1 = p1->next;
        free(p2);
     }    	
}

将链表结点值按照一定的格式输出

void print_T(ptr_TNode head)
{ 
    if(head == NULL){
    printf("链表为空\n");
    return ;
    }
	   ptr_TNode p = head;  
         int     i = 0;
    while (p != NULL){   
        printf("%-5d", p->data);  
            p = p->next; 
        if((++i % 5)==0)
          printf("\n"); 
        
    }
} 

 

以上即是个人学习总结,经过测试过的代码

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值