求一个C语言建立链表的代码
5个回答
展开全部
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 10
typedef struct node *link;
struct node {
int item;
link next;
};
link NODE(int item, link next)
{
link t = malloc(sizeof *t);
t->item = item;
t->next = next;
return t;
}
void insert_node(link head, int item)
{
link t;
for (t = head; t->next; t = t->next)
if (item <= t->next->item)
break;
t->next = NODE(item, t->next);
}
void delete_node(link head, link del_node)
{
link t;
for (t = head; t->next; t=t->next)
if (del_node == t->next){
t->next = t->next->next;
free(del_node);
return;
}
}
void destroy(link head)
{
link q, p;
for (p=head; p; p = q) {
q = p->next;
free(p);
}
}
void show_list(link head)
{
link t;
for (t = head->next; t; t = t->next)
printf("%3d", t->item);
printf("\n");
}
link search_node(link head, int item)
{
link t;
for (t = head; t->next; t = t->next)
if (item == t->next->item)
return t->next;
return NULL;
}
int main(void)
{
int i;
link p = NULL;
/*init head*/
link head = NODE(0, NULL);
srand(time(NULL));
for (i = 0; i < N; i++)
insert_node(head, rand()%100);
insert_node(head, 10);
show_list(head);
printf("delete the node of item is 10\n");
p = search_node(head, 10);
delete_node(head, p);
show_list(head);
destroy(head);
return 0;
}
给你了 求最佳答案
#include <time.h>
#include <stdlib.h>
#define N 10
typedef struct node *link;
struct node {
int item;
link next;
};
link NODE(int item, link next)
{
link t = malloc(sizeof *t);
t->item = item;
t->next = next;
return t;
}
void insert_node(link head, int item)
{
link t;
for (t = head; t->next; t = t->next)
if (item <= t->next->item)
break;
t->next = NODE(item, t->next);
}
void delete_node(link head, link del_node)
{
link t;
for (t = head; t->next; t=t->next)
if (del_node == t->next){
t->next = t->next->next;
free(del_node);
return;
}
}
void destroy(link head)
{
link q, p;
for (p=head; p; p = q) {
q = p->next;
free(p);
}
}
void show_list(link head)
{
link t;
for (t = head->next; t; t = t->next)
printf("%3d", t->item);
printf("\n");
}
link search_node(link head, int item)
{
link t;
for (t = head; t->next; t = t->next)
if (item == t->next->item)
return t->next;
return NULL;
}
int main(void)
{
int i;
link p = NULL;
/*init head*/
link head = NODE(0, NULL);
srand(time(NULL));
for (i = 0; i < N; i++)
insert_node(head, rand()%100);
insert_node(head, 10);
show_list(head);
printf("delete the node of item is 10\n");
p = search_node(head, 10);
delete_node(head, p);
show_list(head);
destroy(head);
return 0;
}
给你了 求最佳答案
展开全部
建立链表可以用头插法也可以用尾插法,以下是用头插法建立带头结点的单链表
#include <stdio.h>
#include <stdlib.h>
#define N 10 //将建立的单链表的结点的个数
typedef struct node{
int item;
struct node *next;
}Node,*Link;
void creat_link(Link linklist,int num)
{//头插法建立单链表
int data,n=0;
Link p,t,q;
printf("请输入第%d个结点元素值:",n+1);
scanf("%d",&data);
if(p=(Link)malloc(sizeof(Node))) n++;
p->item=data;
p->next=NULL;
linklist->next=p;
t=p;
while(n!=num)
{
printf("请输入第%d个结点元素值:",n+1);
scanf("%d",&data);
q=(Link)malloc(sizeof(Node));
q->item=data;
q->next=t;
linklist->next=q;
t=linklist->next;
n++;
}
}
void print(Link linklist)
{//输出单链表
Link p;
p=linklist->next;
while(p!=NULL)
{
printf("%d ",p->item);
p=p->next;
}
}
void main()
{
Link link;
link=(Link)malloc(sizeof(Node));
link->item=N;//头结点item域存储该链表总结点(不包括头结点)个数
link->next=NULL;
creat_link(link,N);
print(link);
}
#include <stdio.h>
#include <stdlib.h>
#define N 10 //将建立的单链表的结点的个数
typedef struct node{
int item;
struct node *next;
}Node,*Link;
void creat_link(Link linklist,int num)
{//头插法建立单链表
int data,n=0;
Link p,t,q;
printf("请输入第%d个结点元素值:",n+1);
scanf("%d",&data);
if(p=(Link)malloc(sizeof(Node))) n++;
p->item=data;
p->next=NULL;
linklist->next=p;
t=p;
while(n!=num)
{
printf("请输入第%d个结点元素值:",n+1);
scanf("%d",&data);
q=(Link)malloc(sizeof(Node));
q->item=data;
q->next=t;
linklist->next=q;
t=linklist->next;
n++;
}
}
void print(Link linklist)
{//输出单链表
Link p;
p=linklist->next;
while(p!=NULL)
{
printf("%d ",p->item);
p=p->next;
}
}
void main()
{
Link link;
link=(Link)malloc(sizeof(Node));
link->item=N;//头结点item域存储该链表总结点(不包括头结点)个数
link->next=NULL;
creat_link(link,N);
print(link);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要头插法还是尾插法建立的链表呢?给你一个引入头结点的尾插法的单链表的吧:
typedef char DataType;
typedef struct node{ //定义结点类型
DataType data; //结点的数据域
struct node * next; //结点的指针域
}ListNode;
typedef ListNode *LinkList;
linkNode* p;
LinkList head;
LinkList CreateList(void)
{
char ch;
LinkList head = (LinkList) malloc(sizeof(ListNode)); //生成头结点
ListNode * s, * r; //工作指针s和尾指针r
r = head; //尾指针初值也指向头结点
while((ch = getchar()) != '\n')
{
s = (ListNode *)malloc(sizeof(ListNode)); //生成新结点
s->data = ch; //将读入的数据放入新结点的数据域中
r->next = s;
r = s;
}
r->next = NULL;//终端结点的指针域置空
return head;
}
typedef char DataType;
typedef struct node{ //定义结点类型
DataType data; //结点的数据域
struct node * next; //结点的指针域
}ListNode;
typedef ListNode *LinkList;
linkNode* p;
LinkList head;
LinkList CreateList(void)
{
char ch;
LinkList head = (LinkList) malloc(sizeof(ListNode)); //生成头结点
ListNode * s, * r; //工作指针s和尾指针r
r = head; //尾指针初值也指向头结点
while((ch = getchar()) != '\n')
{
s = (ListNode *)malloc(sizeof(ListNode)); //生成新结点
s->data = ch; //将读入的数据放入新结点的数据域中
r->next = s;
r = s;
}
r->next = NULL;//终端结点的指针域置空
return head;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-08-03
展开全部
typedef struct node *link;
struct node {
char item;
link next;
}; {/*上面是节点定义*/}
link NODE(char item,link next)
{
link t=malloc(sizeof *t);
t->item=item;
t->=next;
return t;
}
struct node {
char item;
link next;
}; {/*上面是节点定义*/}
link NODE(char item,link next)
{
link t=malloc(sizeof *t);
t->item=item;
t->=next;
return t;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询