單鏈表的初始化、建立、插入、查找、刪除
////////////////////////////////////////////
//單鏈表的初始化,建立,插入,查找,刪除。 //
//Author:Wang Yong //
//Date: 2010.8.19 //
////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
////////////////////////////////////////////
//定義結點類型
typedef struct Node
{
ElemType data; //單鏈表中的數據域
struct Node *next; //單鏈表的指針域
}Node,*LinkedList;
////////////////////////////////////////////
//單鏈表的初始化
LinkedList LinkedListInit()
{
Node *L;
L = (Node *)malloc(sizeof(Node)); //申請結點空間
if(L == NULL) //判斷是否有足夠的內存空間
printf("申請內存空間失敗/n");
L->next = NULL; //將next設置爲NULL,初始長度爲0的單鏈表
}
////////////////////////////////////////////
//單鏈表的建立1,頭插法建立單鏈表
LinkedList LinkedListCreatH()
{
Node *L;
L = (Node *)malloc(sizeof(Node)); //申請頭結點空間
L->next = NULL; //初始化一個空鏈表
ElemType x; //x爲鏈表數據域中的數據
while(scanf("%d",&x) != EOF)
{
Node *p;
p = (Node *)malloc(sizeof(Node)); //申請新的結點
p->data = x; //結點數據域賦值
p->next = L->next; //將結點插入到表頭L-->|2|-->|1|-->NULL
L->next = p;
}
return L;
}
////////////////////////////////////////////
//單鏈表的建立2,尾插法建立單鏈表
LinkedList LinkedListCreatT()
{
Node *L;
L = (Node *)malloc(sizeof(Node)); //申請頭結點空間
L->next = NULL; //初始化一個空鏈表
Node *r;
r = L; //r始終指向終端結點,開始時指向頭結點
ElemType x; //x爲鏈表數據域中的數據
while(scanf("%d",&x) != EOF)
{
Node *p;
p = (Node *)malloc(sizeof(Node)); //申請新的結點
p->data = x; //結點數據域賦值
r->next = p; //將結點插入到表頭L-->|1|-->|2|-->NULL
r = p;
}
r->next = NULL;
return L;
}
////////////////////////////////////////////
//單鏈表的插入,在鏈表的第i個位置插入x的元素
LinkedList LinkedListInsert(LinkedList L,int i,ElemType x)
{
Node *pre; //pre爲前驅結點
pre = L;
int tempi = 0;
for (tempi = 1; tempi < i; tempi++)
pre = pre->next; //查找第i個位置的前驅結點
Node *p; //插入的結點爲p
p = (Node *)malloc(sizeof(Node));
p->data = x;
p->next = pre->next;
pre->next = p;
return L;
}
////////////////////////////////////////////
//單鏈表的刪除,在鏈表中刪除值爲x的元素
LinkedList LinkedListDelete(LinkedList L,ElemType x)
{
Node *p,*pre; //pre爲前驅結點,p爲查找的結點。
p = L->next;
while(p->data != x) //查找值爲x的元素
{
pre = p;
p = p->next;
}
pre->next = p->next; //刪除操作,將其前驅next指向其後繼。
free(p);
return L;
}
/////////////////////////////////////////////
int main()
{
LinkedList list,start;
printf("sssssssssssss\n");
list = LinkedListCreatT();
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("\n");
int i;
ElemType x;
printf("entry the pos to insert");
scanf("%d",&i);
printf("entry insert value");
scanf("%d",&x);
LinkedListInsert(list,i,x);
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("/n");
printf("entry the value to del");
scanf("%d",&x);
LinkedListDelete(list,x);
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("/n");
return 0;
}
//單鏈表的初始化,建立,插入,查找,刪除。 //
//Author:Wang Yong //
//Date: 2010.8.19 //
////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
////////////////////////////////////////////
//定義結點類型
typedef struct Node
{
ElemType data; //單鏈表中的數據域
struct Node *next; //單鏈表的指針域
}Node,*LinkedList;
////////////////////////////////////////////
//單鏈表的初始化
LinkedList LinkedListInit()
{
Node *L;
L = (Node *)malloc(sizeof(Node)); //申請結點空間
if(L == NULL) //判斷是否有足夠的內存空間
printf("申請內存空間失敗/n");
L->next = NULL; //將next設置爲NULL,初始長度爲0的單鏈表
}
////////////////////////////////////////////
//單鏈表的建立1,頭插法建立單鏈表
LinkedList LinkedListCreatH()
{
Node *L;
L = (Node *)malloc(sizeof(Node)); //申請頭結點空間
L->next = NULL; //初始化一個空鏈表
ElemType x; //x爲鏈表數據域中的數據
while(scanf("%d",&x) != EOF)
{
Node *p;
p = (Node *)malloc(sizeof(Node)); //申請新的結點
p->data = x; //結點數據域賦值
p->next = L->next; //將結點插入到表頭L-->|2|-->|1|-->NULL
L->next = p;
}
return L;
}
////////////////////////////////////////////
//單鏈表的建立2,尾插法建立單鏈表
LinkedList LinkedListCreatT()
{
Node *L;
L = (Node *)malloc(sizeof(Node)); //申請頭結點空間
L->next = NULL; //初始化一個空鏈表
Node *r;
r = L; //r始終指向終端結點,開始時指向頭結點
ElemType x; //x爲鏈表數據域中的數據
while(scanf("%d",&x) != EOF)
{
Node *p;
p = (Node *)malloc(sizeof(Node)); //申請新的結點
p->data = x; //結點數據域賦值
r->next = p; //將結點插入到表頭L-->|1|-->|2|-->NULL
r = p;
}
r->next = NULL;
return L;
}
////////////////////////////////////////////
//單鏈表的插入,在鏈表的第i個位置插入x的元素
LinkedList LinkedListInsert(LinkedList L,int i,ElemType x)
{
Node *pre; //pre爲前驅結點
pre = L;
int tempi = 0;
for (tempi = 1; tempi < i; tempi++)
pre = pre->next; //查找第i個位置的前驅結點
Node *p; //插入的結點爲p
p = (Node *)malloc(sizeof(Node));
p->data = x;
p->next = pre->next;
pre->next = p;
return L;
}
////////////////////////////////////////////
//單鏈表的刪除,在鏈表中刪除值爲x的元素
LinkedList LinkedListDelete(LinkedList L,ElemType x)
{
Node *p,*pre; //pre爲前驅結點,p爲查找的結點。
p = L->next;
while(p->data != x) //查找值爲x的元素
{
pre = p;
p = p->next;
}
pre->next = p->next; //刪除操作,將其前驅next指向其後繼。
free(p);
return L;
}
/////////////////////////////////////////////
int main()
{
LinkedList list,start;
printf("sssssssssssss\n");
list = LinkedListCreatT();
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("\n");
int i;
ElemType x;
printf("entry the pos to insert");
scanf("%d",&i);
printf("entry insert value");
scanf("%d",&x);
LinkedListInsert(list,i,x);
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("/n");
printf("entry the value to del");
scanf("%d",&x);
LinkedListDelete(list,x);
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("/n");
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.