如何创建单链表
我只知道如下编码,然后写不出什么了。。。谢谢,给我个模板让我能够借鉴,学习下
#define DATATYPE2 char
typedef struct node
{
DATATYPE2 data;
struct node *next;
}LINKLIST; 展开
typedef struct node{
int data;
struct node *next;
}node;
node *create()
{
node *head,*p,*q;
int i=0;
int x;
head=(node *)malloc(sizeof(node));
while(1)
{
printf("please input the node:");
scanf("%d",&x);
if(x==0) {break;}
p=(node *)malloc(sizeof(node));
p->data=x;
if(++i==1)
{
head->next=p;
}
else
{
q->next=p;
}
q=p;
}
q->next=NULL;
return head;
}
void print(node *head)
{
node *p;
int index=0;
if(head->next==NULL)
{
printf("Link is empty!\n");
exit(0);
}
p=head->next;
while(p!=NULL)
{
printf("the %d node is %d\n",++index,p->data);
p=p->next;
}
}
int length(node *head)
{
int len=0;
node *p;
p=head->next;
while(p)
{
len++;
p=p->next;
}
return len;
}
node *search(node *head,int pos)
{
node *p;
int len=length(head);
p=head->next;
if(pos<0)
{
printf("incorrect position!\n");
return NULL;
}
else if(pos>len)
{
printf("incorrect position!\n");
return NULL;
}
else if(pos==0)
{
return head;
}
if(p==NULL)
{
printf("the link is empty!\n");
return NULL;
}
while (--pos)
{
p=p->next;
}
return p;
}
node *delete(node *head,int pos)
{
node *p,*q;
int len=length(head);
p=head->next;
if(pos<0)
{
printf("incorrect position!\n");
return NULL;
}
else if(pos>len)
{
printf("incorrect position!\n");
return NULL;
}
if(p==NULL)
{
printf("link empty!\n");
return NULL;
}
p=search(head,pos-1);
if(p!=NULL&&p->next!=NULL)
{
q=p->next;
p->next=q->next;
free(q);
}
return head;
}
node *insert(node *head,int pos,int x)
{
node *p,*q=NULL;
q=(node *)malloc(sizeof(node));
q->data=x;
if(pos==0)
{
head->next=q;
return head;
}
p=search(head,pos);
if(p!=NULL)
{
q->next=p->next;
p->next=q;
}
return head;
}
建立单链表的常用方法有两种:头插法建表、尾插法建表
建立单链表的常用方法有两种。下面以顺序存储为例来叙述。
(1) 头插法建表
该方法从一个空表开始,读取数组a中的字符,生成新结点,将读取的数据存放到新结点的数据域中,然后将新结点插入到当前链表的表头上,直到结束为止。算法如下:
void CreateListF(Snode *&L, ElemType a[], int n)
{ Snode *s; int i;
L = (Snode *) malloc(sizeof(Snode));
L->next = NULL;
for (i=0; i<n;i++)
{ s = (Snode *)malloc(sizeof(Snode));
s->data = a[i];
s->next = L->next;
L->next = s;
}
}
(2) 尾插法建表
头插法建立链表虽然算法简单,但生成的链表中结点的次序和原数组元素的顺序相反,若希望两者次序一致,可采用尾插法。该方法是将新结点插到当前链表的表尾上,为此必须增加一个尾指针r,使其始终指向当前链表的尾结点。算法如下:
void CreateListR(Snode *&L, ElemType a[], int n)
{ Snode *s, *r; int i;
L = (Snode *) malloc(sizeof(Snode));
L->next = NULL;
r = L;
for (i=0; i<n;i++)
{ s = (Snode *)malloc(sizeof(Snode));
s->data = a[i];
r->next = s;
r = s;
}
r-> next = NULL;
}
# include <malloc.h>
# include <stdlib.h>
typedef struct Node
{
int data;
struct Node * pNext;
}NODE,* PNODE;
PNODE create_list(void);//创建一个链表
void traverse_list(PNODE pHead);//遍历链表
int main(void)
{
PNODE pHead = NULL;
printf("创建链表:\n");
pHead = create_list();
printf("遍历链表:\n");
traverse_list(pHead);
return 0;
}
PNODE create_list(void)
{
int len;//链表中节点数目
int i;
int val;
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if (NULL == pHead)
{
printf("分配内存失败!程序终止!");
exit(-1);
}
PNODE pTail = pHead;
pTail->pNext = NULL;
printf("请输入要创建的链表中节点的个数len=");
scanf("%d",&len);
for (i=0;i<len;++i)
{
printf("请输入第%d个节点的有效数据:",i+1);
scanf("%d",&val);
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("分配内存失败!程序终止!");
exit(-1);
}
pNew->data = val;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
void traverse_list(PNODE pHead)
{
PNODE p = pHead->pNext ;
while (NULL != p)
{
printf("%10d",p->data);
p = p->pNext;
}
printf("\n");
return ;
}
/*
在vc++6.0中的输出结果:
-----------
创建链表:
请输入要创建的链表中节点的个数len=3
请输入第1个节点的有效数据:1
请输入第2个节点的有效数据:2
请输入第3个节点的有效数据:3
遍历链表:
1 2 3
-----------------
*/
供你参考@ 呵呵
typedef struct node{
int data;
struct node *next;
}node;
node *create()
{
node *head,*p,*q;
int i=0;
int x;
head=(node *)malloc(sizeof(node));
while(1)
{
printf("please input the node:");
scanf("%d",&x);
if(x==0) {break;}
p=(node *)malloc(sizeof(node));
p->data=x;
if(++i==1)
{
head->next=p;
}
else
{
q->next=p;
}
q=p;
}
q->next=NULL;
return head;
}
//用前插法,逐步创建链表(使得结点入链)
void EnLink1(int num,int score,char name[10])
{
Node *newnode;
newnode=new Node;
newnode->num=num;
newnode->score=score;
strcpy(newnode->name,name);
newnode->next=NULL;
if(IsEmpty())
head=newnode;
else
{
newnode->next=head;
head=newnode;
}
}