(1)画出上述单链表示意图;(2)试写一函数删除单链表中所有结点信息为负数的结点。
请各位大侠解答
设有一以head为头指针的带头结点的单链表,链表中的结点信息均为整数
答案:2 悬赏:50 手机版
解决时间 2021-02-23 16:46
- 提问者网友:留有余香
- 2021-02-23 10:15
最佳答案
- 五星知识达人网友:煞尾
- 2021-02-23 11:23
include <stdio.h>#include <stdio.h>
typedef int elemtype;
typedef struct linknode
{
elemtype data;
struct linknode *next;
}nodetype;
nodetype* create()
{
elemtype d;
nodetype *h = NULL,*s,*t;
elemtype i = 1;
printf("create link Node:\n");
while (1)
{
printf("please input %d node data is:",i++);
scanf("%d",&d);
if (d == 0)
{
break;
}
if (d == 1)
{
//创建链表的标头
h = (nodetype*)malloc(sizeof(nodetype));
h->data = d;
h->next=NULL;
//t指向链表标头
t = h;
}
else
{
//性创建个节点
s = (nodetype*)malloc(sizeof(nodetype));
s->data = d;
s->next = NULL;
//把新节点添加到链表中
t->next = s;
//t指向到最后结点
t = s;
}
}
return h;
}
void disp(nodetype *head)
{
nodetype *temp = head;
printf("output this link Node:\n");
if (temp == NULL) //空链表判断
{
printf("this link is NULL!\n");
}
while (temp) //逐个结点输出
{
printf("%d,",temp->data);
temp = temp->next;
}
printf("\n");
}
void deletenode(nodetype *p) //从p中删除结点q
{
nodetype *q;
if (p->next != NULL || p->data<0)
{
q = p->next; //使得q为p的直接后继
p->next = q->next; //先保存p后继结点的后继,删除p后面的直接后继结点q
free(q); //释放空间
}
}
int main()
{
nodetype *head;
head = create(); //创建链表
disp(head); //显示链表
disp(head); //显示链表
return 0;
}
typedef int elemtype;
typedef struct linknode
{
elemtype data;
struct linknode *next;
}nodetype;
nodetype* create()
{
elemtype d;
nodetype *h = NULL,*s,*t;
elemtype i = 1;
printf("create link Node:\n");
while (1)
{
printf("please input %d node data is:",i++);
scanf("%d",&d);
if (d == 0)
{
break;
}
if (d == 1)
{
//创建链表的标头
h = (nodetype*)malloc(sizeof(nodetype));
h->data = d;
h->next=NULL;
//t指向链表标头
t = h;
}
else
{
//性创建个节点
s = (nodetype*)malloc(sizeof(nodetype));
s->data = d;
s->next = NULL;
//把新节点添加到链表中
t->next = s;
//t指向到最后结点
t = s;
}
}
return h;
}
void disp(nodetype *head)
{
nodetype *temp = head;
printf("output this link Node:\n");
if (temp == NULL) //空链表判断
{
printf("this link is NULL!\n");
}
while (temp) //逐个结点输出
{
printf("%d,",temp->data);
temp = temp->next;
}
printf("\n");
}
void deletenode(nodetype *p) //从p中删除结点q
{
nodetype *q;
if (p->next != NULL || p->data<0)
{
q = p->next; //使得q为p的直接后继
p->next = q->next; //先保存p后继结点的后继,删除p后面的直接后继结点q
free(q); //释放空间
}
}
int main()
{
nodetype *head;
head = create(); //创建链表
disp(head); //显示链表
disp(head); //显示链表
return 0;
}
全部回答
- 1楼网友:大漠
- 2021-02-23 12:27
//此题适用计数排序
#include
#include
typedef struct node
{
int num;
struct node * next;
}node, *list;
list listinit(list head, int n)
{
list p;
int i;
for(i = 0; i < n; i ++)
{
p = (list)malloc(sizeof(node));
p->next = head;
head = p;
}
return head;
}
list listreleas(list head)
{
list p = head;
while(head)
{
p = head;
head=p->next;
free(p);
}
return head;
}
int main()
{
list head = null, p = null;
int count[3] = , n, inum;
printf("输入节点数:");
scanf("%d", &n);
head = listinit(head, n);
printf("输入每个节点值(共%d个,只接受-1,0,1):\n", n);
p = head;
while( p != null )
{
scanf("%d", &p->num);
if(p->num < 2 && p->num > -2)
p = p->next;
}
//以下为排序(计数排序)
p = head;
while( p != null )
{
count[p->num + 1] ++;
p = p->next;
}
p = head;
inum = -1;
while( p != null )
{
if(count[inum + 1])
{
p->num = inum;
p = p->next;
count[inum + 1]--;
}
else
inum ++;
}
//以下为输出
p = head;
while( p != null )
{
printf("%d ", p->num);
p = p->next;
}
printf("\n");
listreleas(head);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯