求C语言大神解一下这道简单的链表题

求C语言大神解一下这道简单的链表题,第1张

/*Description

给定一串数字,用链表结构进行存储。然后给定针对该链表的若干插入 *** 作,要求将执行插入 *** 作后的结果输出。

Input

第一行:输入一个整数n,表示这串数字有n个(n大于等于1)。

第二行:输入这n个整数。

第三行:输入一个整数m,表示需要执行m个插入 *** 作。

后面m行:每行输入两个整数a和b,表示在这串数字的当前第a个数字之后插入数字b。(假设链表第一个节点编号为1)

Output

输出 *** 作后的n+m个数字。每个数字用空格空开。

Sample Input

3

2 1 3

2

1 5

1 6

Sample Output

2 6 5 1 3

HINT

最后一个输出数字的后面没有空格

*/

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

//节点结构

struct Node

{

int data //数据

Node *next //指向下一个节点

}

//创建链表头部,iData为数据

Node * CreateHead(int iData)

{

Node *pNode = new Node

if (NULL == pNode) return NULL

pNode->data = iData

pNode->next = NULL

return pNode

}

//释放内存

void ClearList(Node *pHead)

{

Node *pNode = pHead

while(NULL != pNode)

{

Node *pNext = pNode->next

delete pNode

pNode = pNext

}

}

//打印链表数据

void PrintList(Node *pHead)

{

Node *pFindNode = pHead

printf("\n-----------------链表数据打印--------------------\n")

while(NULL != pFindNode)

{

printf("%d\t", pFindNode->data)

pFindNode = pFindNode->next

}

printf("\n----------------------END------------------------\n")

}

//插入节点函数,每次都返回链表头节点

Node *InsertData(int index, int data, int size, Node *pHead)

{

int i = 0

Node *pFindNode = NULL

Node *pNode = NULL

//参数检查

if (NULL == pHead) return CreateHead(data)

if((index <1) || (index >size)) return NULL

//创建新节点

pNode = new Node

if(NULL == pNode) return NULL

pNode->data = data

pNode->next = NULL

//定位插入节点

pFindNode = pHead

while(((index--) >1) &&(NULL != pFindNode->next))pFindNode = pFindNode->next

//执行插入 *** 作

Node *pNext = pFindNode->next

pFindNode->next = pNode

pNode->next = pNext

return pHead

}

int main(void)

{

int i = 0 //链表索引

int iData = 0 //节点存储数据

Node *pHead = NULL //链表首部

int N = 0 //链表初始个数

int M = 0 //插入数据的个数

int size = 0 //链表的当前数据个数

//输入链表的初始元素个数

printf("请输入链表的容量(正整数>0):")

scanf("%d", &N)

//插入初始的数据

printf("请输入%d个整数(以空格分开):", N)

for(i = 0i <N++i)

{

scanf("%d", &iData)

pHead = InsertData(i, iData, size, pHead)//开始插入节点,初次会创建头部

size++//当前链表元素个数增1

}

//输入插入 *** 作的次数

printf("请输入插入整数的个数(正整数>0):")

scanf("%d", &M)

//执行插入 *** 作

while((M--) >0)

{

printf("请输入一组插入 *** 作(1<=index<=%d data):", N)

scanf("%d%d", &i,&iData)

pHead = InsertData(i, iData, size, pHead)

size++

}

//打印数据

PrintList(pHead)

//清空内存

ClearList(pHead)

getch()

return 0

}

#include <stdio.h>

#include <malloc.h>

typedef struct Node

{

int data

struct Node *next

}*LinkedList

void Print(LinkedList list, char *s) {

Node *p = list

if(p == NULL) {

printf("There is no item in %s list", s)

return

}

while(p) {

printf("%d", p->data)

if(p->next) printf(" ")

p = p->next

}

}

void Clear(LinkedList *list){

Node *p

while(*list != NULL) {

p = *list

*list = p->next

free(p)

}

}

void main( )

{

LinkedList A, B, C1, C2

Node *alast, *blast, *c1last, *c2last, *newNode, *p, *q

int data

A = B = C1 = C2 = alast = blast = c1last = c2last = NULL

scanf("%d", &data)

while(data != -1) {

if((data >0) &&(!alast || data >alast->data)) {

newNode = (Node*) malloc (sizeof(Node))

newNode->data = data

if(!A) A = newNode

else alast->next = newNode

alast = newNode

}

scanf("%d", &data)

}

if(alast) alast->next = NULL

scanf("%d", &data)

while(data != -1) {

if((data >0) &&(!blast || data >blast->data)) {

newNode = (Node*) malloc (sizeof(Node))

newNode->data = data

if(!B) B = newNode

else blast->next = newNode

blast = newNode

}

scanf("%d", &data)

}

if(blast) blast->next = NULL

p = A, q = B

while(p) {

data = p->data

while(q &&q->data <data) q = q->next

if(!q) break

if(q->data >data) {

newNode = (Node*) malloc (sizeof(Node))

newNode->data = data

if(!C1) C1 = newNode

else c1last->next = newNode

c1last = newNode

}

p = p->next

}

while(p) {

data = p->data

newNode = (Node*) malloc (sizeof(Node))

newNode->data = data

if(!C1) C1 = newNode

else c1last->next = newNode

c1last = newNode

p = p->next

}

if(c1last) c1last->next = NULL

p = A, q = B

while(p) {

data = p->data

while(q &&q->data <data) q = q->next

if(!q) break

if(q->data == data) {

newNode = (Node*) malloc (sizeof(Node))

newNode->data = data

if(!C2) C2 = newNode

else c2last->next = newNode

c2last = newNode

}

p = p->next

}

if(c2last) c2last->next = NULL

printf("\nA:")

Print(A, "A")

printf("\nB:")

Print(B, "B")

printf("\nC1:")

Print(C1, "C1")

printf("\nC2:")

Print(C2, "C2")

printf("\n")

}

int main()

{

int n

int i

char s[10]

double p

Book *head,*end

head=NULL

printf("Input n:")

scanf("%d",&n)

head=(Book*)malloc(sizeof(Book))

end=head

printf("Input the name,price of the 1 book:")

gets(head->BookName)

scanf("%lf",&p)

head->price=p

end=head->Next

for(i=1i<ni++)

{

end=(Book*)malloc(sizeof(Book))

printf("Input the name,price of the %d book:",i+1)

gets(end->BookName)

scanf("%lf",&p)

end->price=p

end=end->Next

}

end->Next=NULL

double MaxPrice,MinPrice

char MaxName[10],MinName[10]

MaxPrice=MinPrice=head->price

strcpy(MaxName,head->BookName)

strcpy(MinName,head->BookName)

for(i=0i<ni++)

{

if (head->price>MaxPrice)

{ MaxPrice=head->price

strcpy(MaxName,head->BookName)

}

else if(head->price<MinPrice)

{

MinPrice=head->price

strcpy(MinName,head->BookName)

}

head=head->Next

}

printf("The book woth the max price:%s,%lf/n",MaxName,MaxPrice)

printf("The book woth the min price:%s,%lf/n",MinName,MinPrice)

free(head)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8067526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存