单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node *next;
}Node;
Node *Creat_List();
void Traverse_List(Node *head);
void Insert_List(register Node **linkp, int new_value);
int main (int argc, char **argv)
{
Node *head = NULL;
head = Creat_List();
Traverse_List(head);
Insert_List(&head, 5);
Traverse_List(head);
printf ("\n");
return 0;
}
Node *Creat_List()
{
int i = 0;
int len = 0;
int data;
Node *head = (Node*)malloc(sizeof(Node));
Node *tail = head;
tail->next = NULL;
printf ("Please input the number of node.\n");
scanf ( "%d", &len );
for ( i=0; i<len; i++)
{
printf ("Please input the %d integer.\n", i+1);
scanf ( "%d", &data );
Node *current = (Node *)malloc(sizeof(Node));
if ( NULL == current )
{
perror("malloc error:");
exit(1);
}
current->value = data;
tail->next = current;
current->next = NULL;
tail = current;
}
return head;
}
void Traverse_List(Node *head)
{
Node *p = head->next;
while ( NULL != p )
{
printf ("%d ", p->value);
p = p->next;
}
printf ("\n");
}
void Insert_List(register Node **linkp, int new_value)
{
//在函数的指针变量中增加了register声明,用于提高结果代码的效率
register Node *current;register Node *new;
//寻找正确的插入位置,方法是按序询问链表,直到到达一个其值大于或等于新值得节点。
while ( (current=*linkp) != NULL && current->value < new_value )
{
linkp = ¤t->next;
}
new = (Node *)malloc(sizeof(Node));
if ( NULL == new )
{
exit(2);
}
new->value = new_value;
new->next = current;
*linkp = new;
}
void Delete_List(Node *L, int x)
{
Node *current;
Node *pre;
current = L->next;
while ( x != current->value )
{
pre = current;
current = current->next;
}
pre->next = current->next;
free(current);
}
void ReverseList(pNode pHead)
{
if ( NULL != pHead)
{
if ( NULL != pHead->pNext )
{
ReverseList(pHead->pNext);
}
printf ("%d ", pHead->member);
}
}
另外一种就是要求只遍历一次,将单链表中的元素顺序反过来
Node *ReverseList(Node *head)
{
Node *pReverse_head = NULL;
Node *pCurr = head->next;
Node *pNext = NULL;
Node *pPre = NULL;
while ( NULL != pCurr )
{
pNext = pCurr->next;
if ( NULL==pNext )
{
pReverse_head = pCurr;
}
pCurr->next = pPre;//将当前节点的下一节点置为前节点
pPre = pCurr;
pCurr = pNext;
}
Node *pNewhead = (Node *)malloc(sizeof(Node));
pNewhead->next = pReverse_head;
return pNewhead;
}
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。
#include <stdio.h>
#include <stdlib.h>typedef struct node{
int value;
struct node *prior;
struct node *next;
}Dnode;
Dnode *Creat_Dlist();
void Show_Dlist(Dnode *head);
Dnode *insert_Dlist(Dnode *head, int m);
Dnode *delete_Dlist(Dnode *head, int m);
int main (int argc, char **argv)
{
int k = 0;
Dnode *head;
head = Creat_Dlist();
Show_Dlist(head);
printf ("Please input the data you want to be deleted.\n");
scanf ( "%d", &k );
delete_Dlist(head, k);
Show_Dlist(head);
return 0;
}
Dnode *Creat_Dlist()
{
int i = 0;
int n = 0;
Dnode *head;
Dnode *p1;
Dnode *p2;
head = (Dnode *)malloc(sizeof(Dnode));
head->prior = NULL;
head->next = NULL;
p2 = head;
printf ("Please input num of data.\n");
scanf ( "%d", &n );
for ( i=0; i<n; i++)
{
p1 = (Dnode *)malloc(sizeof(Dnode));
printf ("Please input the data.\n");
scanf ( "%d", &(p1->value) );
p2->next = p1;
p1->prior = p2;
p1->next = NULL;
p2 = p1;
}
p1->next = NULL;
return head;
}
void Show_Dlist(Dnode *head)
{
Dnode *p = head->next;
while ( p )
{
printf ("%d ", p->value);
p = p->next;
}
printf ("\n");
}
Dnode *insert_Dlist(Dnode *head, int m)
{
Dnode *p;
Dnode *temp;
p = head;
temp = (Dnode *)malloc(sizeof(Dnode));
temp->value = m;
if ( m<p->value )
{
head = temp;
head->next = p;
head->prior = NULL;
p->prior = head;
return head;
}
while ( p != NULL && m>p->value )
{
p = p->next;
}
if ( m<p->value )
{
temp->next = p;
temp ->prior = p->prior;
p ->prior->next = temp;
p ->prior = temp;
return head;
}
else
{
p->next = temp;
temp ->prior = p;
temp ->next = NULL;
return head;
}
}
Dnode *delete_Dlist(Dnode *head, int m)
{
Dnode *p;
p = head;
if ( m==p->value )
{
head = p->next;
head->prior = NULL;
free(p);
return head;
}
while ( p )
{
if ( m==p->value )
{
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
return head;
}
p = p->next;
}
printf ("data not found.\n");
return head;
}