用C语言实现单链表的各种基本操作
3条回答 默认 最新
- PGEva 2016-10-29 13:15关注
typedef struct node
{
int n;
struct node next;
}Node;
Node *create();
void print(Node *head);
Node *sort(Node *head);
Node *swap(Node *head);
void main()
{
Node *head = create();
print(head);
// head = sort(head);
// print(head);
//head = swap(head);
//print(head);
}
Node *create()
{
Node *head=NULL,*p1=NULL,*p2=NULL;
p1 = (Node)malloc(sizeof(Node));
p1->next = NULL;
while(1 == scanf("%d",&p1->n))
{
if(head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (Node*)malloc(sizeof(Node));
p1->next = NULL;
}
free(p1);
p1 = NULL;
return head;
}
void print(Node *head)
{
Node *p = head;
while(p != NULL)
{
printf("%d ",p->n);
p = p->next;
}
printf("\n");
}
Node *sort(Node *head)
{
Node *newhead=NULL,*newtail=NULL,*p,*min,*minf;
if(head == NULL || head->next == NULL)
return head;
while(head != NULL)
{
p = min = head;
for(;p->next != NULL;p = p->next)
{
if(p->next->n < min->n)
{
min = p->next;
minf = p;
}
}
//2
if(newhead==NULL)
{
newhead = newtail = min;
}
else
{
newtail->next = min;
newtail = min;
}
//3
if(min == head)
head = head->next;
else
minf->next = min->next;
}
if(newhead != NULL)
newtail->next = NULL;
return newhead;
}
Node *swap(Node *head)
{
Node *p = NULL,*pf = NULL,*pn = NULL;
if(head == NULL || head->next == NULL)
return head;
pf = head;
p = pf->next;
pf->next = NULL;
while(p->next != NULL)
{
pn = p->next;
p->next = pf;
pf = p;
p = pn;
}
p->next = pf;
head = p;
return head;
}void test(Node *head)
{
Node *p1,*p2;
p1 = p2 = head;
while(p1 != NULL)
{
p1 = p1->next;
if(p1!=NULL)
{
p1 = p1->next;
p2 = p2->next;
}
}
}#endif
typedef struct data
{
int n;
struct data next;
struct data *front;
}Node;
#if 0
Node *create()
{
Node *head=NULL,*p1=NULL,*p2=NULL;
p1 = (Node)malloc(sizeof(Node));
p1->next = NULL;
while(1 == scanf("%d",&p1->n))
{
if(head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (Node*)malloc(sizeof(Node));
p1->next = NULL;
}
free(p1);
p1 = NULL;
p2->next = head;
return head;
}
void print(Node *head)
{
Node *p = head;
do
{
printf("%d ",p->n);
p = p->next;
}while(p != head);
printf("\n");
}
#endifNode create()
{
Node *head=NULL,*p1=NULL,*p2=NULL;
p1 = (Node)malloc(sizeof(Node));
p1->next = NULL;
p1->front = NULL;
while(1 == scanf("%d",&p1->n))
{
if(head == NULL)
head = p1;
else
{
p2->next = p1;
p1->front = p2;
}
p2 = p1;
p1 = (Node*)malloc(sizeof(Node));
p1->next = NULL;
p1->front = NULL;
}
free(p1);
p1 = NULL;
return head;
}
void print(Node *head)
{
Node *p = head,*p1 = NULL;
while(p != NULL)
{
p1= p;
printf("%d ",p->n);
p = p->next;
}
printf("\n");
while(p1 != NULL)
{
printf("%d ",p1->n);
p1 = p1->front;
}
printf("\n");
}
int main()
{
Node *head = create();
print(head);
return 0;
}
看不懂的话可以私我本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥100 求数学坐标画圆以及直线的算法
- ¥100 c语言,请帮蒟蒻写一个题的范例作参考
- ¥15 名为“Product”的列已属于此 DataTable
- ¥15 安卓adb backup备份应用数据失败
- ¥15 eclipse运行项目时遇到的问题
- ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
- ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
- ¥15 自己瞎改改,结果现在又运行不了了
- ¥15 链式存储应该如何解决
- ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站