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);
}