链表的各种操作与代码实现(上节)

1、在链表头部插入节点:

head-“头指针”,仅指向头节点,而不是头节点本身,头节点是链表的第一个节点
图解

//Linked List: Inserting a node at beginning
#include<stdlib.h>
#include<stdio.h>
struct Node {
    int data; 
    struct Node* next;
};
 struct Node* head;// global variable,can be accessed anywhere
 void Insert(int x);
 void Print();
 int main(){
    head=NULL;//empty list
    printf("How many numbers?\n"); 
    int n,i,x; scanf("%d",&n); 
    for(i=0;i<n;i++){
        printf("Enter the number \n"); 
        scanf("%d",&x); 
        head = Insert(head,x); 
        Print(head);
     }
}

Node* Insert(Node* head, int x)
{
    struct Node*temp = (Node*)malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = head;
    if(head != NULL)temp->next = head;
    head = temp;
    return head;
}

void Print(Node* head)
{   
    printf("List is:"); 
    while(head != NULL)
    {
        printf("%d",temp->data); 
        head = head->next;  
    }
    printf("\n"); 
}

2、在链表任意位置插入节点:

在这里插入图片描述
****注意
这里head为指向头节点的指针,并不是头节点本身,头节点是地址为200的节点

#include<stdlib.h>
#include<stdio.h>
struct Node{
    int data; 
    struct Node* next;
}; 
struct Node* head; 
void Print();
void Insert(int data,int n); 
int main(){
    head=NULL;//empty list
    Insert(2,1);//List:2
    Insert(3,2);//List:2,3
    Insert(4,1);//List:4,2,3
    Insert(5,2);//List:4,5,2,3
    Print();
}

void Insert(int data,int n){
    Node* temp1=new Node(); 
    temp1->data=data;
    temp1->next=NULL; 
    if(n==1){
        temp1->next=head; 
        head=temp1; 
        return; 
    }
    Node* temp2=head; 
    for(int i=e;i<n-2;i++){
        temp2=temp2->next; 
    }
    temp1->next=temp2->next;
    temp2->next=temp1;
}

void Print(){
    Node* temp=head; 
    while(temp!=NULL){
        printf("%d",temp->data); 
        temp=temp->next; 
    }
    printf("\n");
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

内存四区:

在这里插入图片描述
应用程序内存的一部分用于存储所有需要被执行的指令(Code/Text),另一部分用来存储全局变量(Static/global),贯穿整个程序或者说应用程序的生命周期。
存储器的一部分称为栈(Stack),用于存储所有有关函数调用执行的信息,存储所有局部变量,并且这三个部分的大小是固定的,它们的大小在编译时就决定了。
最后一部分我们称为堆或空闲存储(Heap),大小是不固定的,我们可以请求内存,运行时从堆中读取数据,这就是使用malloc或new运算符所做的事情。

3、在链表任意位置删除一个节点:

在这里插入图片描述

//Linked List: Delete a node at nth position
#include<stdio.h>
#include<stdlib.h>
struct Node {
    int data; 
    struct Node* next;
}; 
struct Node* head;//global 
void Insert(int data);//insert an integer at end of list 
void Print();//print all elements in the list
void Delete(int n);//Delete node at position n 
int main()
{
    head=NULL;//empty list 
    Insert(2);
    Insert(4);
    Insert(6); 
    Insert(5);//List:2,4,6,5
    Print(); 
    int n; 
    printf("Enter a position\n"); 
    scanf("%d",&n); 
    Delete(n); 
    Print();
}

//Deletes node at position n 
void Delete(int n)
{
    struct Node* temp1=head; 
    if(n==1){
        head=temp1->next;//head now points to second node.
        free(temp1); 
        return;
     }
     int i; 
     for(i=0;i<n-2;i++)
         temp1=templ->next;
     //templ points to(n-1) th Node 
     struct Node* temp2=temp1->next;//nth Node 
     temp1->next=temp2->next;//(n+1) th Node
     free(temp2);//delete temp2
}
 
//temp1->next等价与(*temp1).next

4、链表:反转一个链表(迭代):

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//Reverse a nexted list
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data; 
struct Node* next;
}; 
struct Node* Reverse(struct Node* head)
{
    struct Node * current,* prev,* next; 
    current=head; 
    preV=NULL; 
    while(current !=NULL)
    {
        next=current->next; 
        current->next=prev; 
        prev=current; 
        current=next;
    }
    head=prev; 
    return head;
}

int main()
{
    struct Node* head =NULL;//local variable 
    head=Insert(head,2);// Insert:struct Node* Insert(struct Node* head,int data)
    head=Insert(head,4);
    head=Insert(head,6);
    head=Insert(head,8); 
    Print(head); 
    head=Reverse(head); 
    Print(head);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-今晚打老虎-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值