首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

新手求带头结点的单链表尾部插结点的函数写法(C语言),该如何解决

2012-02-16 
新手求带头结点的单链表尾部插结点的函数写法(C语言)typedef struct node{int istruct node* next}LNode

新手求带头结点的单链表尾部插结点的函数写法(C语言)
typedef struct node
{
int i;
struct node* next;
}LNode,*LinkList;
LNode* createNode()
{
LNode *p=(LNode*)malloc(sizeof(LNode));
p->next=NULL;
return p;
}
上面是带头结点的单链表的定义和创建单个结点方法,求一个在单链表尾部添加数据结点的函数
int Insert_LinkList_End(LinkList L,int x);要求返回最后的结点个数(头结点不计数,它不放数据。x为数据不是添加的结点个数


[解决办法]
int Insert_LinkList_End(LinkList L,int x)
{
int i=0;
LinkList S,P=L;

while(P->next)
{
P=P->next;
i++; 


S=CreateNode();
S.i=x;

P->next=S;
S->next=NULL;



return ++i;



}
[解决办法]
int Insert_LinkList_End(LinkList L,int x);中写这个

C/C++ code
int Insert_LinkList_End(LinkList L,int x){int count=0;LinkList current=head;while(current->next!=NULL){    current=current->next;    count++;}LinkList newnode=createNode();newnode->i=x;current->next=newnode;return count+1;} 

热点排行