/*---------------------------------------------------------
Title: Completed Simple Linked List
Author: Eman Lee
Date: Oct 22, 2008
Fuction: Operation on Linked Stored Linear List.
This is a completed simple sample.
It is related to Section 2.2.4 in our textbook.(p56-63)
----------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
struct nodetype//Define node 定义节点
{
int data;//Data field
struct nodetype * next;//Pointer field which point to the next node
};
typedef struct nodetype Node;
Node * InitialLinkList()//Initialize a Linked List,and return the head pointer
{
struct nodetype * head;
head=(Node *)malloc(sizeof(Node));//
head->next=NULL;
return head;
}
void CreateLinkListInRear(Node * head,
int a[], int L)
{//Insert new node in the rear of link list.
int i;
Node * t,* rear;
rear=head;
for(i=0;i<L;i++)
{
t=(Node *)malloc(sizeof(Node));
t->data=a[i];
t->next=NULL;
rear->next=t;
rear=t;
}
}
void CreateLinkListInHead(Node * head,
int a[], int L)
{//Insert new node in the front of link list.
int i;
Node * t;
for(i=0;i<L;i++)
{
t=(Node *)malloc(sizeof(Node));
t->data=a[i];
t->next=head->next;
head->next=t;
}
}
Node * SearchInLinkList(Node * head, int x)
{ //Search x in link list.
Node * p=head->next;
while(p!=NULL)
{
if(p->data==x)
return p;
else
p=p->next;
}
return NULL;
}
void InsertNumberIntoLinkList(Node * head,
int key,int x)
{
//Insert x after key in the linked list.
//Need two pointers if insert x before key.
Node * loc,*t;
if((loc=SearchInLinkList(head,key))!=NULL)
{
t=(Node *)malloc(sizeof(Node));
t->data=x;
t->next=loc->next;
loc->next=t;
}
else
printf("\nNot Found, Insert failed!\n");
}
void InsertNumber(Node * head,
int key,int x)
{ //Insert x before key in the linked list.
Node * f,*r,*t;
f=head;
r=head->next;
while(r!=NULL && r->data!=key)//Find key
{
r=r->next;
f=f->next;
}
if(r!=NULL) //Found key
{
t=(Node *)malloc(sizeof(Node));//Insert x
t->data=x;
f->next=t;
t->next=r;
printf("Found key. Inserted Successfully.");
}
else //Not Found key
{
printf("Not Found key.Not inserted.");
}
}
void DeleteNumberFromLinkList(Node * head,int key)
{
//Delete x in the linked list.need two pointers
Node * f,*r;
f=head;
r=head->next;
while(r!=NULL && r->data!=key)//Find key
{
r=r->next;
f=f->next;
}
if(r!=NULL) //Found key
{
f->next=r->next;
free(r);
printf("Found key. Inserted Successfully.\n");
}
else //Not Found key
{
printf("Not Found key.Failde.\n");
}
}
void PrintIntegerLinkList(Node * head)
{//Show nodes on the screen.
Node *t;
t=head->next;
while(t!=NULL)
{
printf("%d ",t->data);
t=t->next;
}
printf("\n");
}
void main()
{
Node *head,*head2;
//int x[5]={1,2,3,4,5};
int y[5]={4,5,6,7,8};
//head=InitialLinkList();
head2=InitialLinkList();
CreateLinkListInHead(head2,y,5);
//CreateLinkListInRear(head,x,5);
PrintIntegerLinkList(head2);
//PrintIntegerLinkList(head);
if(SearchInLinkList(head2,88)!=NULL)
printf("\nFound\n");
else
printf("\nNot Found\n");
//InsertNumberIntoLinkList(head2,7,33);
InsertNumber(head2,4,44);
DeleteNumberFromLinkList(head2,5);
PrintIntegerLinkList(head2);
}
标签:Node,head,单链,示例,int,next,key,2008,NULL
From: https://blog.51cto.com/emanlee/8247621