69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *create() /* 建立链表 */
{
int num;
struct node *head, *tail, *p;
head = tail = NULL;
puts("Please enter a num: ");
scanf("%d",&num);
while(num != 0)
{
p = (struct node *)malloc(sizeof(struct node));
p -> data = num;
p -> next = NULL;
if(head == NULL)
head = tail = p;
else
{
tail -> next = p;
tail = p;
}
scanf("%d",&num);
}
return (head);
}
int main(void)
{
struct node *point,*temp;
point = create();
puts("The list ");
while(point != NULL) /* 读取链表 */
{
temp = point;
printf("%d\t",point -> data);
point = point -> next;
free(temp);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *create() /* 建立链表 */
{
int num;
struct node *head, *tail, *p;
head = tail = NULL;
puts("Please enter a num: ");
scanf("%d",&num);
while(num != 0)
{
p = (struct node *)malloc(sizeof(struct node));
p -> data = num;
p -> next = NULL;
if(head == NULL)
head = tail = p;
else
{
tail -> next = p;
tail = p;
}
scanf("%d",&num);
}
return (head);
}
void print_link(struct node* head)
{
struct node* point = head;
puts("The list ");
while(point != NULL)
{
printf("%d\t",point -> data);
point = point -> next;
}
printf("\n");
}
void free_link(struct node* head)
{
struct node* point = head;
while(point != NULL)
{
struct node* t = point;
point = point -> next;
free(t);
}
}
struct node* split_link(int split_pos, struct node* head)
{
struct node* point = head;
struct node* prevp = head;
struct node* newp = NULL;
int pos = 0;
if (split_pos <= 0)
return NULL;
while(point != NULL)
{
if (pos == split_pos)
break;
prevp = point;
point = point -> next;
++pos;
}
if (point != NULL)
{
newp = point;
prevp->next = NULL;
}
return newp;
}
void merge_link( struct node* p1, struct node* p2)
{
struct node* point = p1;
if (!p1 || !p2 || !point)
return;
while(point->next != NULL)
{
point = point->next;
}
point->next = p2;
}
int main(void)
{
struct node *point,*point2;
point = create();
print_link(point);
point2 = split_link(3, point);
print_link(point);
print_link(point2);
merge_link(point, point2);
print_link(point);
free_link(point);
return 0;
}
---------
Please enter a num:
1 2 3 4 5 6 7 8 9 0
The list
1 2 3 4 5 6 7 8 9
The list
1 2 3
The list
4 5 6 7 8 9
The list
1 2 3 4 5 6 7 8 9
请按任意键继续. . .