C语言手写单向链表与双向链表

#include <stdio.h>
#include <stdlib.h>
/*
单向链表
*/
typedef struct Node {
	int data;
	struct Node* next;
}Node;
//头节点前插入
Node * Insert_toHead(Node* head ,int x) {
	Node* temp = ( Node*)malloc(sizeof(Node));
	temp->data = x;
	temp->next = head;
	head = temp;
    return head;
}
//自定义位置插入
Node * Insert_toAnyWhere(Node* head ,int x,int index) {
	struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data=x;
    temp->next=NULL;
    int i=1;
    Node *p=head;
    while(p!=NULL){
        if(i==index-1){
           Node *q=p->next;
           p->next=temp;
           temp->next=q;
           printf("insert OK\n");
           break;
        }
        i++;
        p=p->next;
    }
     if(i!= index-1){
        printf("insert faile\n");
     }
     return head;
}
//链表尾部插入
Node * insert_toEnd(Node * head,int x){
  Node * temp =(Node *)malloc(sizeof(Node));
  temp->data=x;
  temp->next=NULL;
  if(head == NULL){
     head = temp;
  }else{
    Node *p=head;
     while(p->next!=NULL){
        p=p->next;
     }
     p->next=temp;
  }
   return head;
}
//删除某个节点
Node * delete_Node(Node *head,int x){
    Node *p = head;
    while(p->next->data!=x && p!=NULL){
          p=p->next;
    }
   if(p!=NULL){
    Node *temp = p->next;
    p->next=temp->next;
    free(temp);
   }
    return head;
}
//遍历链表
void Show(Node* head) {
   printf("MyList is :"); 
	struct Node* p = head;
	while (p!= NULL) {
		printf("%d\t",p->data);
		p = p->next;
	}
    printf("\n");
}
//反转单向链表
Node * recvList(Node * head){
     Node * p=head->next;
     head->next=NULL;
     Node *q=NULL;
     while(p!=NULL){
        q=p->next;
        p->next=head;
        head=p;
        p=q;
     }
     return head;
}
/*
双向链表
*/
typedef struct two_Node{
    int data;
   struct two_Node * front;
   struct two_Node * next;
}TNode;
//向头节点插入数据
TNode * Insert_toHead_forTNode(TNode * head,int x){
       TNode * temp =(TNode *)malloc(sizeof(TNode));
       temp->data=x;
       temp->front=NULL;
       if(head != NULL){
       temp->next=head;
       head->front=temp;
       }else{
        temp->front=NULL;
        temp->next =NULL;
       }
        head=temp;
       return head;
}
//自定义位置插入
TNode * Insert_toAnyWhere_forTNode(TNode * head,int x,int index){
       TNode * temp =(TNode *)malloc(sizeof(TNode));
       temp->data=x;
       temp->front=NULL;
       temp->next =NULL;
       TNode * p= head;
       int i=1;
       while (p!=NULL){
          if(i==index-1)break;
          p=p->next;
          i++;
       }
       if(i==index -1){
       TNode * q= p->next;
       p->next=temp;
       temp->front=p;
       q->front=temp;
       temp->next=q;
       }
       return head;
}
//在尾部插入数据
TNode * Insert_toEnd_forTNode(TNode * head,int x){
       TNode * temp =(TNode *)malloc(sizeof(TNode));
       temp->data=x;
       temp->front=NULL;
       temp->next =NULL;
       TNode * p= head;
       while(p->next!=NULL){
        p=p->next;
       }
       p->next=temp;
       temp->front=p;
       return head;
}
//删除某个节点
TNode * delete_TNode(TNode * head,int x){
       TNode *p=head;
       while(p->data!=x&&p!=NULL){
        p=p->next;
        }
        TNode * temp1=p->front,* temp2=p->next;
        p->next=NULL;
        p->front=NULL;
        temp1->next=NULL;
        temp1->next=temp2;
        if(temp2!=NULL){
        temp2->front=NULL;
        temp2->front=temp1;
        }
        free(p);
       return head;
}
//反转双向链表
TNode * recvTNode(TNode *head){
    TNode * p =head->next;
    head->next=NULL;
    TNode *q =NULL;
    while(p!=NULL){
        q=p->next;
        head->front=p;
        p->next=head;
        head=p;
        p=q;
    }
  return head;
}
//遍历双向链表
void Show_forTNode(TNode * head){
      TNode *p=head;
      printf("The TNodeList is:");
      while(p != NULL){
        printf("%d\t",p->data);
        p=p->next;
      }
      printf("\n");
}
int main() {
	TNode* head = NULL;
	printf("how many number?\n");
	int n,x;
	scanf("%d",&n);
	for (int i = 0; i < n; i++) {
		printf("Enter the number \n");
		scanf("%d", &x);
	head=Insert_toHead_forTNode(head,x);
	}
	Show_forTNode(head);
    head=Insert_toAnyWhere_forTNode(head,1000,3);
    Show_forTNode(head);
    Insert_toEnd_forTNode(head,100);
    Show_forTNode(head);
    head=delete_TNode(head,1000);
    Show_forTNode(head);
    head=delete_TNode(head,100);
    Show_forTNode(head);
    head=recvTNode(head);
     Show_forTNode(head);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值