数据结构与算法分析-C语言描述编程题

第三章表栈和队列

链表.h

#pragma once
pragma once
#include 
#include 
typedef struct node
{
	struct node* next;
	int date;
} Node;
Node* creat(int n) {
	Node* head, * node, * end;
	head = (Node*)malloc(sizeof(Node));
	end = head;
	for (int i = 0; i < n; i++)
	{
		node = (Node*)malloc(sizeof(Node));
		scanf_s("%d", &node->date);
		end->next = node;
		end = node;
	}
	end->next = NULL;//结束创建
	return head;
}
void pr_list(Node* head)
{
	while (head)
	{
		printf("%d ", head->date);
		head = head->next;
	}
}
void freeList(Node* head)
{
	while (head)
	{
		Node* tmp = head;
		head = head->next;
		free(tmp);
	}
	printf("\n");
}

3.1

#include
#include
typedef struct Node {
	int date;
	struct Node* next;
}Node;
Node* creat(int n) {
	Node* head, * node, * end;
	head = (Node*)malloc(sizeof(Node));
	end = head;
	for (int i = 0; i < n; i++)
	{
		node = (Node*)malloc(sizeof(Node));
		scanf_s("%d", &node->date); 
		end->next = node;
		end = node;
	}
	end->next = NULL;//结束创建
	return head;
}
void printf(Node* h)
{
	while (h->next != NULL) {
		h = h->next;
		printf("%d  ", h->date);
	}
}
int main()
{
	Node* head = creat(5);
	printf(head);
	return 0;
}

3.2

#include
#include
typedef struct Node {
	int date;
	struct Node* next;
}Node;
Node* creat(int n) {
	Node* head, * node, * end;
	head = (Node*)malloc(sizeof(Node));
	end = head;
	for (int i = 0; i < n; i++)
	{
		node = (Node*)malloc(sizeof(Node));
		scanf_s("%d", &node->date);
		end->next = node;
		end = node;
	}
	end->next = NULL;//结束创建
	return head;
}
void printf(Node* h)
{
	while (h->next != NULL) {
		h = h->next;
		printf("%d  ", h->date);
	}
}
void PrintLots(Node *L1, Node *L2)
{
	Node *p1, *p2;
	p2 = L2->next;
	while (p2 != NULL)
	{
		int i = 0;
		p1 = L1->next;
		while (i < p2->date - 1 && p1 != NULL)
		{
			p1 = p1->next;
			i++;
		}
		printf("%d ", p1->date);
		p2 = p2->next;
	}
}
int main()
{
	Node* L = creat(3);
	Node* p = creat(5);
	printf(L);
	printf(p);
	PrintLots(p, L);
	return 0;
}

3.3 a.

#include 
#include 
typedef struct node
{
	struct node* next;
	int date;
} Node;
Node* swap(Node* head1)
{
	if (head1 == NULL) return head1;
	Node* q = head1, * pre = NULL;
	while (q && q->next)
	{
		Node* p1 = q, * p2 = q->next;
		q = p2->next;

		p1->next = p2->next;
		p2->next = p1;
		if (pre == NULL) head1 = p2;
		else		 pre->next = p2;
		pre = p1;
	}
	return head1;
}
Node* createList(int* s, int len)
{
	int i = 0;
	Node* head = NULL, * tail = NULL;
	for (i = 0; i < len; ++i)
	{
		Node* newNode = (Node*)malloc(sizeof(Node));
		newNode->date = s[i];
		newNode->next = NULL;
		if (tail == NULL)
		{
			head = tail = newNode;
		}
		else
		{
			tail->next = newNode;
			tail = newNode;
		}
	}
	return head;
}
void pr_list(Node* head)
{
	while (head)
	{
		printf("%d ", head->date);
		head = head->next;
	}
}
void freeList(Node* head)
{
	while (head)
	{
		Node* tmp = head;
		head = head->next;
		free(tmp);
	}
	printf("\n");
}
int main(void) {
	int s[] = { 1,2,3,4};
	int len = sizeof(s) / sizeof(*s);
	Node* head = createList(s, len);
	head = swap(head);
	pr_list(head);
	freeList(head);
	return 0;
}

3.4

#include 
#include 
typedef int datatype;
typedef struct node_list{
    datatype infor;
    struct node_list *next;
}node;
 
//  单链表的创建,带头结点。
node * creat1()
{
    node *s,*tail;
    datatype x;
    node * head=(node *)malloc(sizeof(node));
    head->next=NULL;
    tail=head;
    printf("Please enter an information \n");
    scanf("%d",&x);
    while( x!=-1)
    {
        s=(node *)malloc(sizeof(node));
        s->infor=x;
        tail->next=s;
        tail=s;
        scanf("%d",&x);
    }
    tail->next=NULL;
    return head;
}
//  单链表的遍历
 void display(node * head)
 {   node * p;
     p=head;
     while(p!=NULL)
     {
         printf("%5d",p->infor);
         p=p->next;
     }
     printf("\n");
 }
 void display1(node * head)
 {   node * p;
     p=head->next;
     while(p!=NULL)
     {
         printf("%5d",p->infor);
         p=p->next;
     }
     printf("\n");
 }
 
//核心功能函数,找出交集
node *Intersect(node *head1,node *head2)
{
  node *p1=head1,*p2=head2;
  node *head,*p,*q;
  head = (node *)malloc(sizeof(node));  //创建带头结点的单链表。
  head->next = NULL;
  p = head;
  while( p1 && p2 )
  {
    if (p1->infor == p2->infor)
     {
      q = (node*)malloc(sizeof(node));
      q->infor = p1->infor;
      q->next = NULL;
      p->next = q;
      p = q;
      p1 = p1->next;
      p2 = p2->next;
     }
    else if (p1->infor < p2->infor)
     {
       p1 = p1->next;
      }
   else
   {
     p2 = p2->next;
   }
  }
    return head;
}
  int main()
 {
     node * head1,*head2,*head3;
     head1=creat1();
     display1(head1);
     head2=creat1();
     display1(head2);
     head3=Intersect(head1,head2);
     puts("The intersection of two single chain tables is ");
     display1(head3);
     return 0;
 }

3.5

#include 
#include 
typedef int ElementType;
typedef struct Node* PtrToNode;
struct Node {
	ElementType Data;
	PtrToNode Next;
};
typedef PtrToNode List;
List Read();
void Print(List L); 
List Merge(List L1, List L2);
int main()
{
	List L1, L2, L;
	L1 = Read();
	L2 = Read();
	L = Merge(L1, L2);
	Print(L);
	Print(L1);
	Print(L2);
	return 0;
}
List Merge(List L1, List L2)
{
	List L, p1, p2, r;
	p1 = L1->Next;
	p2 = L2->Next;
	L = (List)malloc(sizeof(struct Node));
	L->Next = NULL;
	r = L;
	while (p1 && p2) {
		if (p1->Data < p2->Data) {
			r->Next = p1;
			r = p1;
			p1 = p1->Next;
		}
		else {
			r->Next = p2;
			r = p2;
			p2 = p2->Next;
		}
	}
	if (p1)
		r->Next = p1;
	else
		r->Next = p2;
	L1->Next = NULL;
	L2->Next = NULL;
	return L;
}
List Read()
{
	int n, i;
	List L, p, s;
	scanf_s("%d", &n);
	L = (List)malloc(sizeof(struct Node));
	L->Next = NULL;
	p = L;
	for (i = 0; i < n; ++i) {
		s = (List)malloc(sizeof(struct Node));
		scanf_s("%d", &s->Data);
		s->Next = p->Next;
		p->Next = s;
		p = s;
	}
	return L;
}
void Print(List L)
{
	List p;
	p = L->Next;
	if (L->Next == NULL)
	{
		printf("NULL");
	}
	while (p)
	{
		printf("%d ", p->Data);
		p = p->Next;
	}
	printf("\n");
}

3.6

#include 
#include 
typedef struct Node {
    float xishu;			
    int zhishu;					
    struct Node* Next;
}Node;
typedef struct Node* PNode;	
void insertNewPoint_link(PNode head, PNode qNode)
{
    PNode p = head;				
    PNode h = head;
    PNode q;					
    while (p->Next != NULL)		
    {
        
        if (p->Next->zhishu < qNode->zhishu)
        {
            qNode->Next = p->Next;
            p->Next = qNode;
            break;				
        }
        else if (p->Next->zhishu == qNode->zhishu)
        {
            float sum = p->Next->xishu + qNode->xishu;
            if (sum != 0)	
            {
                p->Next->xishu = sum;
            }
            else		
            {
                q = p->Next;
                p->Next = p->Next->Next;
                free(q);
                q = NULL;
            }
            break;
        }
        p = p->Next;			
    }
    if (p->Next == NULL)		
    {
        p->Next = qNode;
    }
}


void printLinkeLink(PNode head)
{
    PNode temp = head->Next;				
    printf("%fX^%d", temp->xishu, temp->zhishu);	
    temp = temp->Next;		
    while (temp != NULL)		
    {
        if (temp->xishu > 0)	
            printf(" +%fX^%d", temp->xishu, temp->zhishu);
        else if (temp->xishu < 0)	
            printf("%fX^%d", temp->xishu, temp->zhishu);
        temp = temp->Next;
    }
}

//多项式的加法计算
void add_poly(Node* pa, Node* pb)//pa,pb分别为多项式一和多项式二的头指针
{
    Node* p = pa->Next;		//p为遍历指针,此时指向多项式一的第一项
    Node* q = pb->Next;		//q为遍历指针,此时指向多项式二的第一项
    Node* pre = pa;			//pre此刻指向多项式一的头指针,后续作为中间载体
    Node* u;				//u指针做临时指针,用于释放节点
    while (p != NULL && q != NULL)//若指针指向的内容都不为空
    {
        if (p->zhishu > q->zhishu)//若多项式一中的项系数大于对应多项式二中的项
        {
            pre = p;
            p = p->Next;
        }
        else if (p->zhishu == q->zhishu)//若两项系数相等则合并同类项
        {
            float x = p->xishu + q->xishu;//x为合并后的系数
            if (x != 0)				//若合并后系数不为零
            {
                p->xishu = x;		//将合并后的系数赋给多项式一中对应的项
                pre = p;				//pre指向p结点
            }
            else					//若合并后系数为零
            {
                pre->Next = p->Next;//指向下一个结点
                free(p);			//释放p销毁结点
            }
            p = pre->Next;
            u = q;
            q = q->Next;
            free(u);
        }
        else				//若多项式一中的项系数小于对应多项式二中的项
        {
            u = q->Next;
            q->Next = p;
            pre->Next = q;
            pre = q;
            q = u;
        }
    }
    if (q)
    {
        pre->Next = q;
    }
    free(pb);
}

//实现主函数
void main()
{
    float xishu;		//定义变量  系数和指数
    int zhishu;
    PNode head1 = (PNode)malloc(sizeof(struct Node));
    PNode head2 = (PNode)malloc(sizeof(struct Node));
    PNode tem = NULL;
    head1->Next = NULL;
    head2->Next = NULL;
    //输入多项式一各项
    printf("输入链表一的系数和指数,如:3,2 (以0,0结束输入):\n");
    scanf_s("%f,%d", &xishu, &zhishu);
    while (xishu != 0)
    {
        tem = (PNode)malloc(sizeof(struct Node));	//往链表中插入新的项
        tem->xishu = xishu;
        tem->zhishu = zhishu;
        tem->Next = NULL;
        insertNewPoint_link(head1, tem);			//插入新结点
        scanf_s("%f,%d", &xishu, &zhishu);			//输入下一项
    }
    printf("多项式一为:\n");
    printLinkeLink(head1);						
    printf("\n");
    printf("\n输入链表二的系数和指数,如:3,2 (以0,0结束输入):\n");
    scanf_s("%f,%d", &xishu, &zhishu);
    while (xishu != 0)
    {
        tem = (PNode)malloc(sizeof(struct Node));
        tem->xishu = xishu;
        tem->zhishu = zhishu;
        tem->Next = NULL;
        insertNewPoint_link(head2, tem);
        scanf_s("%f,%d", &xishu, &zhishu);
    }
    printf("多项式二为:\n");
    printLinkeLink(head2);
    printf("\n");
    add_poly(head1, head2);
    printf("\n多项式相加后的结果为:\n");
    printLinkeLink(head1);
    printf("\n\n");
}

3.7

#include   
#include   

typedef struct node
{
    float coef;//系数  
    int exp;    //指数  
    struct node* next; //指针域  
}listnode;


listnode* CreateList(int n);
int printflist(listnode* head);
int InverseList(listnode* head);
listnode* MultiplisePoly(listnode* head_a, listnode* head_b);

int main()
{
    printf("  链表实现多项式的乘法   \n");
    int n;
    printf("请输入A(X)的项数(降幂排列)\n");
    scanf_s("%d", &n);
    listnode* head_a = CreateList(n);
    printf("A(X)=");
    printflist(head_a);
    printf("请输入B(X)的项数(降幂排列)\n");
    scanf_s("%d", &n);
    listnode* head_b = CreateList(n);
    //  InverseList(head_b);  
    printf("B(X)=");
    printflist(head_b);
    listnode* head_c = MultiplisePoly(head_a, head_b);
    printf("C(X)=A(X)*B(X)=");
    printflist(head_c);

    return 0;
}


listnode* CreateList(int n)
{
    listnode* head = (listnode*)malloc(sizeof(listnode)), * p, * pre = head;//head指向头结点,p指向新开辟的节点  
    float coef; //系数  
    int exp;    //指数  
    if (NULL == head)
    {
        printf("开辟头结点失败\n");
        exit(-1);
    }
    head->next = NULL;
    for (int i = 0; i < n; i++)
    {
        if (NULL == (p = (listnode*)malloc(sizeof(listnode))))
        {
            printf("新结点malloc失败\n");
            exit(-1);
        }
        printf("请输入第%d个系数和指数\n", i + 1);
        scanf_s("%f,%d", &coef, &exp);
        p->coef = coef;
        p->exp = exp;  //更新节点数据  
        p->next = NULL;
        //插入节点  
        pre->next = p;
        pre = p;
    }
    return  head;   //这里是返回堆的内存区 不是局部变量  
}

int printflist(listnode* head)
{
    listnode* p = head->next;
    while (p->next != NULL)
    {
        printf("%1.1f*X^%d+", p->coef, p->exp);// %1.1f格式输出小数点后只保留一位  
        p = p->next;
    }
    printf("%1.1f*X^%d\n", p->coef, p->exp);
    return 0;
}

/************************************************************************/
/* 链式相乘:
思路:
1.因为两个链表都是指数递减,所以A(X)递减,B(x)逆置下,递增,why do this?
2.先获取两个最大的指数和 exp_max. 这样的话余下的指数就是都在0~7之间了。
    关键来了,遍历相乘本质并不难,但是如何可以找到所有的指数呢?而且还要开辟新的节点来存储没有的指数
解决:用一个新的链来存储结果,从exp_max开始向下查找,每一个可能指数都要遍历到。
这里指数升序+降序的排列就很精妙了。
    for(k=exp_max;k>=0;k--)
    {
        相乘;
        判断是否还有同类的系数,有就相加;
    }
如何判断呢?就是在步进查找。
若是当前k值,表明该指数找到了,此时就是a,b都后继一位,因为只有这种组合才可能有同样系数
若是当前指数k,表明要减少系数和,只有b增加
 这也就看出了,a,b两个链表指数一个升序一个降序的好处了。这种思路很好

3.归纳总结下:
 3.1 求k=exp_max;
 3.2 逆置b
 3.3 遍历查找 怎么做循环又是个问题
  一旦查找到了 =k的情况,然后就继续搜索其他可能性 直到都到NULL节点
                                                                    */
                                                                    /************************************************************************/
listnode* MultiplisePoly(listnode* head_a, listnode* head_b)//链式相乘  
{
    listnode* head_c, * pa = head_a, * pb = head_b, * pc, * newnode;
    int exp_max; //指数之和最大值  
    if (pa->next != NULL && pb->next != NULL)
        exp_max = pa->next->exp + pb->next->exp; //获取最大指数和   
    else return NULL;
    //初始化链表C头结点  
    head_c = (listnode*)malloc(sizeof(listnode));
    if (NULL == head_c)
    {
        printf("开辟链表C失败\n");
        exit(-1);
    }
    head_c->coef = 0.0;
    head_c->exp = 0;
    head_c->next = NULL;
    pc = head_c;
    InverseList(head_b);    //逆置b链表  
    float ceof = 0.0;
    for (int k = exp_max; k >= 0; k--)
    {
        pa = head_a->next; //恢复pa的指向  
        while (pa != NULL && pa->exp > k) //首先查找pa的位置 找不大于k的  
            pa = pa->next;
        pb = head_b->next;//恢复Pb的指向  
        while (pa != NULL && pb != NULL && pa->exp + pb->exp < k)//然后在查找pb的位置 pa+pb的指数和不大于k  
            pb = pb->next;
        //经过上面两轮后 pa+pb 的exp<=k  
        while (pa != NULL && pb != NULL)//此循环进入后,找到所有的同指数的和相加  
        {
            if (k == pa->exp + pb->exp)  //目的就是找等于K  
            {
                ceof += pa->coef * pb->coef;
                pa = pa->next;
                pb = pb->next;
            }
            else
            {
                if (pa->exp + pb->exp < k) //小于k 增加pb  
                    pb = pb->next;
                else
                    pa = pa->next; //大于k 减小pa  
            }

        }
        if (ceof != 0.0)   //有系数了 就将此节点插入到c链表中  
        {
            if (NULL == (newnode = (listnode*)malloc(sizeof(listnode))))
            {
                printf("链表C节点开辟失败");
                exit(-1);
            }
            newnode->coef = ceof;
            newnode->exp = k;
            newnode->next = NULL; //插入节点数据  

            pc->next = newnode;
            pc = newnode;         //插入节点        
            ceof = 0.0;
        }
    }

    InverseList(head_b);
    return head_c;
}

int InverseList(listnode* head) //逆置链表  
{
    listnode* p = head->next, * q; //p指向正要逆置的节点,q指向下一个待逆置的节点  
    head->next = NULL;
    while (p)    //当前节点不为空  
    {
        q = p->next;//保存下一个节点  
        p->next = head->next; //先更新逆置点的 next  
        head->next = p;        //在更新head->next   
        p = q;           //下一轮  
    }
    return 0;
}

3.11

#include"链表.h"
LinkList Find(LinkList p,int x)
{
	LinkList node = p->next;
	if (node!= nullptr &&node->date != x)
		Find(node->next,x);
	return node;
}
int main()
{
	LinkList L;
	int inser_ele;
	LinkList find_node;
	int del_ele=0;
	int pos;
	L=creat(5);
	pr_list(L);
	printf("请输入要查找的元素的值:");
	scanf_s("%d", &pos);
	find_node = Find(L,pos);
	printf("所查找的元素为:%d\n", find_node->date);
	return 0;
}

3.12

#include"链表.h"
void reverse(LinkList& head) {
    if (head == NULL) {
        return;
    }

    LinkList pre,  cur, ne;
    pre = head;
    cur = head->next;
    while (cur) {
        ne = cur->next; // Store next pointer.
        cur->next = pre; // Reverse the current code pointer.
        pre = cur;
        cur = ne;
    }

    head->next = NULL;
    head = pre;
}
int main()
{
    LinkList L;
    int pos;
    printf("初始化链表L,请输入5个数字:");
    L= creat(5);
    pr_list(L);
    reverse(L);
    pr_list(L);
}

3.14

#include
#include
typedef int  VertexType;//顶点类型
typedef int EdgeType;//边上的权值
#define MAXVEX 10

typedef struct EdgeNode//边表结点
{
	int adjvex;			//领结点域,存储该节点的下标
	EdgeType weight;  //用于存储权值
	struct EdgeNode* next; //指向下一个邻接点
}EdgeNode;
typedef struct VertexNode //顶点表结点
{
	VertexType data;    //顶点域,存储顶点信息
	EdgeNode* firstedge;//边表头指针
}VertexNode,AdjList[MAXVEX];
typedef struct {
	AdjList adjlist;
	int numVertexes, numEdges;
}GraphAdjList;//图
void CreateALGraph(GraphAdjList* G)
{
	int i, j, k;
	EdgeNode* e;
	printf("输入顶点和边数:\n");
	scanf_s("%d,%d", &G->numVertexes, &G->numEdges);
	for (i = 0; i < G->numVertexes; i++)
	{
		scanf_s("%d", &G->adjlist[i].data);
		G->adjlist[i].firstedge = NULL;
	}
	for (k = 0; k < G->numEdges; k++)
	{
		printf("输入边(Vi,Vj)上的顶点序号:\n");
		scanf_s("%d,%d",&i,&j);
		e = (EdgeNode*)malloc(sizeof(EdgeNode));
		e->adjvex = j;
		e->next = G->adjlist[i].firstedge;
		G->adjlist[i].firstedge = e;

		e = (EdgeNode*)malloc(sizeof(EdgeNode));
		e->adjvex = i;
		e->next = G->adjlist[j].firstedge;
		G->adjlist[j].firstedge = e;
	}
}

int main()
{
	GraphAdjList GL;
	CreateALGraph(&GL);
	return 0;
}

你可能感兴趣的:(数据结构与算法分析-C语言描述编程题)