#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;
}
C语言手写单向链表与双向链表
于 2023-11-22 21:23:15 首次发布