c语言数据结构单链表的初始化 插入 销毁 元素的取出 删除 操作 求详细C源代码 谢谢。。。。
2个回答
展开全部
单链表功能大全,嘿嘿
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//链表输返者出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}
//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//单链表测长
void length(Node *head)
{
Node *p = head->pstnext;
int j=0;
while(NULL != p)
{
p = p->pstnext;
j++;
}
printf("%d\r\n", j);
}
//链表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
printf("链差世瞎表中没有找到该值");
}else if(date == p->nDate)
{
printf("要查找的值%d在链表中第%d个位置\r\n", date, n);
}
return;
}
//按序号查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf("查找位置不合法\r\n");
}else if(i == 0)
{
printf("查找位置为头结点\r\n");
}else if(i == Num)
{
printf("第%d个位置数据为%d\r\n", i, p->nDate);
}
}
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf("插入位置不存在\r\n");
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\虚空n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
printf("插入位置不存在\r\n");
}
}
//删除指定结点
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
printf("删除位置不存在\r\n");
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
//指定删除单链表中某个数据,并统计删除此数据的个数
int Delete_2(Node *head, int Delete_date)
{
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
{
q = p->pstnext;
if(q->nDate == Delete_date)
{
p->pstnext = q->pstnext;
free(q);
++count;
}
else
{
p = q;
}
}
return count;
}
//链表逆置
void Reverse_list(Node *head)
{
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
{
return;
}
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
{
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
}
}
//单链表的连接
void connect_list(Node *head, Node *head_New)
{
Node *p = head;
while(NULL != p->pstnext)
{
p = p->pstnext;
}
p->pstnext = head_New->pstnext;
}
//单链表销毁
void destroy_list(Node* head)
{
while (NULL != head)
{
Node* temp = head;
head = head->pstnext;
free(temp);
}
}
void main()
{
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
int Delete_date, k; //待删除的数据与其个数
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
printf("输出建立的单链表\r\n");
output(Head);
//单链表测长
printf("单链表长度为\r\n");
length(Head);
//链表按值查找
printf("请输入待查找的数据\r\n");
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
printf("请输入待查找序号\r\n");
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
printf("在指定第i个元素之前插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入后新链表\r\n");
output(Head);
//在指定第i2个元素之后插入新元素Newdate
printf("在指定第i个元素之后插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入后新链表\r\n");
output(Head);
//指定删除i3元素
printf("删除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("删除后新链表\r\n");
output(Head);
//指定删除单链表中某个数据,并统计删除此数据的个数
printf("请输入待删除的元素\r\n");
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
printf("删除后新链表\r\n");
output(Head);
printf("删除指定元素在链表中的个数为:");
printf("%d\r\n", k);
//单链表逆置
Reverse_list(Head);
printf("逆置后输出\r\n");
output(Head);
//单链表的连接
printf("建立一个新链表\r\n");
Head_New = creat();
printf("输出新链表");
output(Head);
printf("将新链表连接到原来链表的尾部并输出\r\n");
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);
return;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//链表输返者出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}
//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//单链表测长
void length(Node *head)
{
Node *p = head->pstnext;
int j=0;
while(NULL != p)
{
p = p->pstnext;
j++;
}
printf("%d\r\n", j);
}
//链表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
printf("链差世瞎表中没有找到该值");
}else if(date == p->nDate)
{
printf("要查找的值%d在链表中第%d个位置\r\n", date, n);
}
return;
}
//按序号查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf("查找位置不合法\r\n");
}else if(i == 0)
{
printf("查找位置为头结点\r\n");
}else if(i == Num)
{
printf("第%d个位置数据为%d\r\n", i, p->nDate);
}
}
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf("插入位置不存在\r\n");
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\虚空n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
printf("插入位置不存在\r\n");
}
}
//删除指定结点
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
printf("删除位置不存在\r\n");
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
//指定删除单链表中某个数据,并统计删除此数据的个数
int Delete_2(Node *head, int Delete_date)
{
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
{
q = p->pstnext;
if(q->nDate == Delete_date)
{
p->pstnext = q->pstnext;
free(q);
++count;
}
else
{
p = q;
}
}
return count;
}
//链表逆置
void Reverse_list(Node *head)
{
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
{
return;
}
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
{
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
}
}
//单链表的连接
void connect_list(Node *head, Node *head_New)
{
Node *p = head;
while(NULL != p->pstnext)
{
p = p->pstnext;
}
p->pstnext = head_New->pstnext;
}
//单链表销毁
void destroy_list(Node* head)
{
while (NULL != head)
{
Node* temp = head;
head = head->pstnext;
free(temp);
}
}
void main()
{
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
int Delete_date, k; //待删除的数据与其个数
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
printf("输出建立的单链表\r\n");
output(Head);
//单链表测长
printf("单链表长度为\r\n");
length(Head);
//链表按值查找
printf("请输入待查找的数据\r\n");
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
printf("请输入待查找序号\r\n");
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
printf("在指定第i个元素之前插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入后新链表\r\n");
output(Head);
//在指定第i2个元素之后插入新元素Newdate
printf("在指定第i个元素之后插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入后新链表\r\n");
output(Head);
//指定删除i3元素
printf("删除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("删除后新链表\r\n");
output(Head);
//指定删除单链表中某个数据,并统计删除此数据的个数
printf("请输入待删除的元素\r\n");
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
printf("删除后新链表\r\n");
output(Head);
printf("删除指定元素在链表中的个数为:");
printf("%d\r\n", k);
//单链表逆置
Reverse_list(Head);
printf("逆置后输出\r\n");
output(Head);
//单链表的连接
printf("建立一个新链表\r\n");
Head_New = creat();
printf("输出新链表");
output(Head);
printf("将新链表连接到原来链表的尾部并输出\r\n");
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);
return;
}
展开全部
001 ////////////////////////////////////////////
002 //单链表的初始化,建立,插入,查找,删除。//
003 //Author:Wang Yong //
004 //Date: 2010.8.19 //
005 ////////////////////////////////////////////
006
007
008 #include <stdio.h>
009 #include <stdlib.h>
010
011 typedef int ElemType;
012 ////////////////////////////////////////////
013
014 //定义结点类型
015 typedef struct Node
016 {
017 ElemType data; //单链表中的数据域
018 struct Node *next; //单链表的指针域
019 }Node,*LinkedList;
020
021 ////////////////////////笑槐////////////////////
022
023 //单链表的初始化
024
025 LinkedList LinkedListInit()
026 {
027 Node *L;
028 L = (Node *)malloc(sizeof(Node)); //申请结点空间
029 if(L == NULL) //判断是否有足够的内存空间
030 printf("申请内存空间失败\n");
031 L->next = NULL; //将next设置为NULL,初始长度为0的单链表
032 }
033
034 ////////////////////////////////////////////
035
036 //单链表的建立1,头插法建立单链表
037
038 LinkedList LinkedListCreatH()
039 {
040 Node *L;
041 L = (Node *)malloc(sizeof(Node)); //申请仔升顷头结点空间
042 L->next = NULL; //初始化一个空链表
043
044 ElemType x; //x为链表数据域中的数据
045 while(scanf("%d",&x) != EOF)
046 {
047 Node *p;
048 p = (Node *)malloc(sizeof(Node)); //申请新的结点
049 p->data = x; //结点数据域念陆赋值
050 p->next = L->next; //将结点插入到表头L-->|2|-->|1|-->NULL
051 L->next = p;
052 }
053 return L;
054 }
055
056 ////////////////////////////////////////////
057
058 //单链表的建立2,尾插法建立单链表
059
060 LinkedList LinkedListCreatT()
061 {
062 Node *L;
063 L = (Node *)malloc(sizeof(Node)); //申请头结点空间
064 L->next = NULL; //初始化一个空链表
065 Node *r;
066 r = L; //r始终指向终端结点,开始时指向头结点
067 ElemType x; //x为链表数据域中的数据
068 while(scanf("%d",&x) != EOF)
069 {
070 Node *p;
071 p = (Node *)malloc(sizeof(Node)); //申请新的结点
072 p->data = x; //结点数据域赋值
073 r->next = p; //将结点插入到表头L-->|1|-->|2|-->NULL
074 r = p;
075 }
076 r->next = NULL;
077
078 return L;
079 }
080
081 ////////////////////////////////////////////
082
083 //单链表的插入,在链表的第i个位置插入x的元素
084
085 LinkedList LinkedListInsert(LinkedList L,int i,ElemType x)
086 {
087 Node *pre; //pre为前驱结点
088 pre = L;
089 int tempi = 0;
090 for (tempi = 1; tempi < i; tempi++)
091 pre = pre->next; //查找第i个位置的前驱结点
092 Node *p; //插入的结点为p
093 p = (Node *)malloc(sizeof(Node));
094 p->data = x;
095 p->next = pre->next;
096 pre->next = p;
097
098 return L;
099 }
100
101 ////////////////////////////////////////////
102
103 //单链表的删除,在链表中删除值为x的元素
104
105 LinkedList LinkedListDelete(LinkedList L,ElemType x)
106 {
107 Node *p,*pre; //pre为前驱结点,p为查找的结点。
108 p = L->next;
109 while(p->data != x) //查找值为x的元素
110 {
111 pre = p;
112 p = p->next;
113 }
114 pre->next = p->next; //删除操作,将其前驱next指向其后继。
115 free(p);
116 return L;
117 }
118
119 /////////////////////////////////////////////
120
121 int main()
122 {
123 LinkedList list,start;
124 /* printf("请输入单链表的数据:");
125 list = LinkedListCreatH();
126 for(start = list->next; start != NULL; start = start->next)
127 printf("%d ",start->data);
128 printf("\n");
129 */ printf("请输入单链表的数据:");
130 list = LinkedListCreatT();
131 for(start = list->next; start != NULL; start = start->next)
132 printf("%d ",start->data);
133 printf("\n");
134
135 int i;
136 ElemType x;
137 printf("请输入插入数据的位置:");
138 scanf("%d",&i);
139 printf("请输入插入数据的值:");
140 scanf("%d",&x);
141 LinkedListInsert(list,i,x);
142 for(start = list->next; start != NULL; start = start->next)
143 printf("%d ",start->data);
144 printf("\n");
145 printf("请输入要删除的元素的值:");
146 scanf("%d",&x);
147 LinkedListDelete(list,x);
148 for(start = list->next; start != NULL; start = start->next)
149 printf("%d ",start->data);
150 printf("\n");
151
152 return 0;
153 }
002 //单链表的初始化,建立,插入,查找,删除。//
003 //Author:Wang Yong //
004 //Date: 2010.8.19 //
005 ////////////////////////////////////////////
006
007
008 #include <stdio.h>
009 #include <stdlib.h>
010
011 typedef int ElemType;
012 ////////////////////////////////////////////
013
014 //定义结点类型
015 typedef struct Node
016 {
017 ElemType data; //单链表中的数据域
018 struct Node *next; //单链表的指针域
019 }Node,*LinkedList;
020
021 ////////////////////////笑槐////////////////////
022
023 //单链表的初始化
024
025 LinkedList LinkedListInit()
026 {
027 Node *L;
028 L = (Node *)malloc(sizeof(Node)); //申请结点空间
029 if(L == NULL) //判断是否有足够的内存空间
030 printf("申请内存空间失败\n");
031 L->next = NULL; //将next设置为NULL,初始长度为0的单链表
032 }
033
034 ////////////////////////////////////////////
035
036 //单链表的建立1,头插法建立单链表
037
038 LinkedList LinkedListCreatH()
039 {
040 Node *L;
041 L = (Node *)malloc(sizeof(Node)); //申请仔升顷头结点空间
042 L->next = NULL; //初始化一个空链表
043
044 ElemType x; //x为链表数据域中的数据
045 while(scanf("%d",&x) != EOF)
046 {
047 Node *p;
048 p = (Node *)malloc(sizeof(Node)); //申请新的结点
049 p->data = x; //结点数据域念陆赋值
050 p->next = L->next; //将结点插入到表头L-->|2|-->|1|-->NULL
051 L->next = p;
052 }
053 return L;
054 }
055
056 ////////////////////////////////////////////
057
058 //单链表的建立2,尾插法建立单链表
059
060 LinkedList LinkedListCreatT()
061 {
062 Node *L;
063 L = (Node *)malloc(sizeof(Node)); //申请头结点空间
064 L->next = NULL; //初始化一个空链表
065 Node *r;
066 r = L; //r始终指向终端结点,开始时指向头结点
067 ElemType x; //x为链表数据域中的数据
068 while(scanf("%d",&x) != EOF)
069 {
070 Node *p;
071 p = (Node *)malloc(sizeof(Node)); //申请新的结点
072 p->data = x; //结点数据域赋值
073 r->next = p; //将结点插入到表头L-->|1|-->|2|-->NULL
074 r = p;
075 }
076 r->next = NULL;
077
078 return L;
079 }
080
081 ////////////////////////////////////////////
082
083 //单链表的插入,在链表的第i个位置插入x的元素
084
085 LinkedList LinkedListInsert(LinkedList L,int i,ElemType x)
086 {
087 Node *pre; //pre为前驱结点
088 pre = L;
089 int tempi = 0;
090 for (tempi = 1; tempi < i; tempi++)
091 pre = pre->next; //查找第i个位置的前驱结点
092 Node *p; //插入的结点为p
093 p = (Node *)malloc(sizeof(Node));
094 p->data = x;
095 p->next = pre->next;
096 pre->next = p;
097
098 return L;
099 }
100
101 ////////////////////////////////////////////
102
103 //单链表的删除,在链表中删除值为x的元素
104
105 LinkedList LinkedListDelete(LinkedList L,ElemType x)
106 {
107 Node *p,*pre; //pre为前驱结点,p为查找的结点。
108 p = L->next;
109 while(p->data != x) //查找值为x的元素
110 {
111 pre = p;
112 p = p->next;
113 }
114 pre->next = p->next; //删除操作,将其前驱next指向其后继。
115 free(p);
116 return L;
117 }
118
119 /////////////////////////////////////////////
120
121 int main()
122 {
123 LinkedList list,start;
124 /* printf("请输入单链表的数据:");
125 list = LinkedListCreatH();
126 for(start = list->next; start != NULL; start = start->next)
127 printf("%d ",start->data);
128 printf("\n");
129 */ printf("请输入单链表的数据:");
130 list = LinkedListCreatT();
131 for(start = list->next; start != NULL; start = start->next)
132 printf("%d ",start->data);
133 printf("\n");
134
135 int i;
136 ElemType x;
137 printf("请输入插入数据的位置:");
138 scanf("%d",&i);
139 printf("请输入插入数据的值:");
140 scanf("%d",&x);
141 LinkedListInsert(list,i,x);
142 for(start = list->next; start != NULL; start = start->next)
143 printf("%d ",start->data);
144 printf("\n");
145 printf("请输入要删除的元素的值:");
146 scanf("%d",&x);
147 LinkedListDelete(list,x);
148 for(start = list->next; start != NULL; start = start->next)
149 printf("%d ",start->data);
150 printf("\n");
151
152 return 0;
153 }
参考资料: 把行数删了吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询