数据结构的学习
链表的学习代码
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int num;
struct Node* next;
} Node;
int main() {
//=======build LinkedList=============
int len=10;
Node *head,*temp,*tail;
for (int i = 0; i < len; ++i) {
Node *n=malloc(sizeof(Node));
n->num=i;
if(i==0){
head=n;
temp=head;
}
else{
temp->next=n;
temp=n;
}
}
tail=temp;
temp->next=NULL;
//=======append Node to LinkedList=======
Node *newNode=malloc(sizeof(Node));
newNode->num=25;
tail->next=newNode;
newNode->next=NULL;
tail=newNode;
//====remove node from LinkedList====
temp=head;
int index=0;
Node *t;
if(index>0){
for (int i = 0; i < index-1; ++i) { ;
temp=temp->next;
}
t=temp->next;
temp->next=temp->next->next;
free(t);
} else if(index==0){
t=head;
head=head->next;
free(t);
}
temp=head;
while (temp!=NULL){
printf("%d\t%p\n",temp->num,temp);
temp=temp->next;
}
return 0;
}