链表
# ifndef _NODE_H_
# define _NODE_H_
//定义一个Node链表类型结构体
typedef struct _node{
int value; //数据
struct _node *next; //指针
} Node;
#endif
---
## 链表结尾插入元素
---
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
//自己定义的数据结构
typedef struct _list{
Node * head;
} List;
void add(List* pList, int number);
int main(int argc, char const argv[]) {
List list;
Node * head = NULL;
int number;
do {
printf("Input: ");
scanf("%d",number);
if( number != -1)
{
//add to linked-list
add(&list, number);
}
} while (number != -1);
return 0;
}
void add(List* pList, int number)
{
Node * p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
//find the last
Node * last = pList->head;
if(last)
{
while(last->next)
{
last = last->next;
}
//attach
last->next = p;
}
else
{
pList->head = p;
}
}
//省略遍历才能找到链表末尾
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct _list{
Node * head;
Node * tail;
} List;
void add(List* pList, int number);
int main(int argc, char const argv[]) {
List list;
Node * head = NULL;
int number;
do {
printf("Input: ");
scanf("%d",number);
if( number != -1)
{
//add to linked-list
add(&list, number);
}
} while (number != -1);
return 0;
}
void add(List* pList, int number)
{
Node * p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
//find the last
// Node * last = pList->head;
// if(last)
// {
// while(last->next)
// {
// last = last->next;
// }
// //attach
// last->next = p;
// }
// else
// {
// pList->head = p;
// }
//How to write tail->head ?
}
打印链表内容
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct _list{
Node * head;
// Node * tail;
} List;
void add(List* pList, int number);
void print(List *pList);
int main(int argc, char const argv[]) {
List list;
Node * head = NULL;
int number;
do {
printf("Input: ");
scanf("%d",number);
if( number != -1)
{
//add to linked-list
add(&list, number);
}
} while (number != -1);
print(&list);
return 0;
}
void add(List* pList, int number)
{
Node * p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
//find the last
Node * last = pList->head;
if(last)
{
while(last->next)
{
last = last->next;
}
//attach
last->next = p;
}
else
{
pList->head = p;
}
}
void print(List *pList)
{
Node * p;
for(p=pList->head; p ; p=p->next)
{
printf("%d\t",p->value);
}
printf("\n");
}
删除指定链表元素 & 清除链表
输入一个数字并在链表中找到该数字且删除
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct _list{
Node * head;
// Node * tail;
} List;
int isFound;
void add(List* pList, int number);
void print(List *pList);
int main(int argc, char const argv[]) {
List list;
Node * head = NULL;
int number;
do {
printf("Input: ");
scanf("%d",number);
if( number != -1)
{
//add to linked-list
add(&list, number);
}
} while (number != -1);
print(&list);
//find element
Node * p;
for(p=list.head; p ; p=p->next)
{
if(p->value == number)
{
printf("找到了\n");
}
isFound = 1;
break;
}
if(!isFound)
{
printf("没有找到\n");
}
//find element and delete it
Node *q;
for(q=NULL,p=list.head; p; q=p, p=p->next)
{
if(p->value == number)
{
if(q)
{
q->next = p->next;
}
else
{
list.head = p->next;
}
free(p);
break;
}
}
//delete all list
for(p=head; p; p=q)
{
q=p->next;
free(p);
}
return 0;
}
void add(List* pList, int number)
{
Node * p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
//find the last
Node * last = pList->head;
if(last)
{
while(last->next)
{
last = last->next;
}
//attach
last->next = p;
}
else
{
pList->head = p;
}
}
void print(List *pList)
{
Node * p;
for(p=pList->head; p ; p=p->next)
{
printf("%d\t",p->value);
}
printf("\n");
}


