日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MFC版本链表演示程序

發布時間:2025/4/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MFC版本链表演示程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一?

鏈表是學習C語言時的重要內容。網上都有很多鏈表的示例,一般都是控制臺版本的。其輸入輸出顯示在控制臺。

下面鄙人把鏈表的輸出顯示在MFC單文檔窗口。

1 首先在VC中創建一個單文檔應用程序

加入如下菜單資源


2 把鏈表相關定義添加到視圖類頭文件

把鏈表的結構體定義添加到視圖類頭文件;把鏈表的操作函數作為視圖類的成員函數;

3 為菜單添加命令處理函數

4 在視圖類中

實現鏈表的操作函數;實現在窗口上輸出鏈表的操作結果;


完成2,3,4,之后的視圖類頭文件和實現文件如下;VC自動生成的其他類未做修改;

// lianbiaodemoView.h : interface of the CLianbiaodemoView class // /#if !defined(AFX_LIANBIAODEMOVIEW_H__36AD083C_F5FE_4FD5_A5F4_78898A5AEC67__INCLUDED_) #define AFX_LIANBIAODEMOVIEW_H__36AD083C_F5FE_4FD5_A5F4_78898A5AEC67__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000struct grade {int score;struct grade *next; }; typedef struct grade NODE;class CLianbiaodemoView : public CView { protected: // create from serialization onlyCLianbiaodemoView();DECLARE_DYNCREATE(CLianbiaodemoView)// Attributes public:CLianbiaodemoDoc* GetDocument();struct grade *head,*pnew;int m; // Operations public:struct grade *create(); //創建鏈表void insert(NODE *head,NODE *pnew,int i); //插入鏈表void pdelete(NODE *head,int i); //刪除列表void Pfree(NODE *head); //銷毀鏈表// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CLianbiaodemoView)public:virtual void OnDraw(CDC* pDC); // overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);//}}AFX_VIRTUAL// Implementation public:virtual ~CLianbiaodemoView(); #ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const; #endifprotected:// Generated message map functions protected://{{AFX_MSG(CLianbiaodemoView)afx_msg void OnMenuitem32771();afx_msg void OnMenuitem32772();afx_msg void OnMenuitem32773();afx_msg void OnMenuitem32774();//}}AFX_MSGDECLARE_MESSAGE_MAP() };#ifndef _DEBUG // debug version in lianbiaodemoView.cpp inline CLianbiaodemoDoc* CLianbiaodemoView::GetDocument(){ return (CLianbiaodemoDoc*)m_pDocument; } #endif///{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_LIANBIAODEMOVIEW_H__36AD083C_F5FE_4FD5_A5F4_78898A5AEC67__INCLUDED_)
// lianbiaodemoView.cpp : implementation of the CLianbiaodemoView class //#include "stdafx.h" #include "lianbiaodemo.h"#include "lianbiaodemoDoc.h" #include "lianbiaodemoView.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif/ // CLianbiaodemoViewIMPLEMENT_DYNCREATE(CLianbiaodemoView, CView)BEGIN_MESSAGE_MAP(CLianbiaodemoView, CView)//{{AFX_MSG_MAP(CLianbiaodemoView)ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771)ON_COMMAND(ID_MENUITEM32772, OnMenuitem32772)ON_COMMAND(ID_MENUITEM32773, OnMenuitem32773)ON_COMMAND(ID_MENUITEM32774, OnMenuitem32774)//}}AFX_MSG_MAP// Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP()/ // CLianbiaodemoView construction/destructionCLianbiaodemoView::CLianbiaodemoView() {// TODO: add construction code herem=0; }CLianbiaodemoView::~CLianbiaodemoView() { }BOOL CLianbiaodemoView::PreCreateWindow(CREATESTRUCT& cs) {// TODO: Modify the Window class or styles here by modifying// the CREATESTRUCT csreturn CView::PreCreateWindow(cs); }/ // CLianbiaodemoView drawingvoid CLianbiaodemoView::OnDraw(CDC* pDC) {CLianbiaodemoDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereNODE *p;int x=0;char strscore[5];switch(m){case 1:pDC->TextOut(100,100,"新創建的鏈表:"); for (p=head->next; p!=NULL; p=p->next){itoa(p->score,strscore,10);pDC->TextOut(100+x*25,125,strscore);x=x+1;}break;case 2:pDC->TextOut(100,150,"插入后的鏈表:"); for (p=head->next; p!=NULL; p=p->next){itoa(p->score,strscore,10);pDC->TextOut(100+x*25,175,strscore);x=x+1;}break;case 3:pDC->TextOut(100,200,"刪除后的鏈表:"); for (p=head->next; p!=NULL; p=p->next){itoa(p->score,strscore,10);pDC->TextOut(100+x*25,225,strscore);x=x+1;}break;case 4:pDC->TextOut(100,250,"釋放后的鏈表:"); for (p=head->next; p!=NULL; p=p->next){itoa(p->score,strscore,10);pDC->TextOut(100+x*25,275,strscore);x=x+1;}break;default:break;} }/ // CLianbiaodemoView printingBOOL CLianbiaodemoView::OnPreparePrinting(CPrintInfo* pInfo) {// default preparationreturn DoPreparePrinting(pInfo); }void CLianbiaodemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {// TODO: add extra initialization before printing }void CLianbiaodemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {// TODO: add cleanup after printing }/ // CLianbiaodemoView diagnostics#ifdef _DEBUG void CLianbiaodemoView::AssertValid() const {CView::AssertValid(); }void CLianbiaodemoView::Dump(CDumpContext& dc) const {CView::Dump(dc); }CLianbiaodemoDoc* CLianbiaodemoView::GetDocument() // non-debug version is inline {ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLianbiaodemoDoc)));return (CLianbiaodemoDoc*)m_pDocument; } #endif //_DEBUG/ // CLianbiaodemoView message handlers//創建鏈表 void CLianbiaodemoView::OnMenuitem32771() {// TODO: Add your command handler code herehead=create();if (head==NULL)return;m=1;Invalidate(); }//插入 void CLianbiaodemoView::OnMenuitem32772() {// TODO: Add your command handler code herepnew=(NODE *)malloc(sizeof(NODE));if (pnew==NULL) {return ;}pnew->score=87;insert(head,pnew, 1); //將新節點插入節點3的后面m=2;RECT rect1;rect1.left=100;rect1.right=200;rect1.top=150;rect1.bottom=200;InvalidateRect(&rect1,true); }//刪除 void CLianbiaodemoView::OnMenuitem32773() {// TODO: Add your command handler code herepdelete(head,2);m=3;RECT rect1;rect1.left=100;rect1.right=200;rect1.top=200;rect1.bottom=250;InvalidateRect(&rect1,true); }//釋放 void CLianbiaodemoView::OnMenuitem32774() {// TODO: Add your command handler code herePfree(head);m=4;RECT rect1;rect1.left=100;rect1.right=200;rect1.top=250;rect1.bottom=300;InvalidateRect(&rect1,true); }struct grade * CLianbiaodemoView::create() {NODE *head,*tail,*pnew;int score;head=(NODE *)malloc(sizeof(NODE)); //創建頭節點。if (head==NULL) { //創建失敗返回return NULL;}head->next=NULL; //頭節點指針域置NULLtail=head; // 開始時尾指針指向頭節點pnew=(NODE *)malloc(sizeof(NODE)); //創建新節點if (pnew==NULL) { //創建失敗返回return NULL;}score=90;pnew->score=score; //新節點數據域存放輸入的成績pnew->next=NULL; //新節點指針域置NULLtail->next=pnew; //新節點插入到表尾tail=pnew; //為指針指向當前的尾節點return head; //返回創建鏈表的頭指針 }void CLianbiaodemoView::insert(NODE *head,NODE *pnew,int i) {NODE *p; //當前指針int j;p=head;for (j=0; j<i&&p!=NULL; j++) //p指向要插入的第i個節點p=p->next;if (p==NULL) { //節點i不存在return;}pnew->next=p->next; //插入節點的指針域指向第i個節點的后繼節點p->next=pnew; //犟第i個節點的指針域指向插入的新節點 }void CLianbiaodemoView::pdelete(NODE *head,int i) {NODE *p,*q;int j;if (i==0) //刪除的是頭指針,返回return;p=head;for (j=1; j<i&&p->next!=NULL; j++)p=p->next; //將p指向要刪除的第i個節點的前驅節點if (p->next==NULL) { //表明鏈表中的節點不存在printf("不存在!");return;}q=p->next; //q指向待刪除的節點p->next=q->next; //刪除節點i,也可寫成p->next=p->next->nextfree(q); //釋放節點i的內存單元 }void CLianbiaodemoView::Pfree(NODE *head) {NODE *p,*q;p=head;while (p->next!=NULL) {q=p->next;p->next=q->next;free(q);}free(p); }
最后的結果如下:



鏈表的實現可參閱相關資料;
另上面 的MFC程序有幾個要點:
itoa(p->score,strscore,10);
pDC->TextOut(100+x*25,225,strscore);
itoa是轉化整型的成績為字符串,然后輸出;
每輸出一個數字,x坐標增加25,再輸出下一個數字;輸出完一行后y坐標增加25;
Invalidate()刷新整個窗口;
InvalidateRect(&rect1,true)刷新所指定的矩形區域;對于第二次以后的輸出是刷新局部區域,否則前一次的輸出就會沒有了;


上述工程下載
http://pan.baidu.com/s/1o8qyWLs
文件名
lianbiaodemo


二 控制臺版鏈表

#include<stdio.h> #include<stdlib.h> typedef struct k { int data; struct k*next; }node; node*creatlink() { node *head,*tail,*q; int x; head=tail=NULL; printf("please input zhengshu,endby 0:\n"); scanf("%d",&x); while(x!=0) { q=(node*)malloc(sizeof(node)); q->data=x; if(head==NULL) { head=tail=q; } else { tail->next=q; tail=q; } scanf("%d",&x); } if(tail!=NULL) tail->next=NULL; return head; } void print(node*head) { node*p=head; printf("List is:\n"); while(p!=NULL) { printf("%5d",p->data); p=p->next; } printf("\n"); } int main() { node*head; head=creatlink(); print(head); getchar(); return 0; }




總結

以上是生活随笔為你收集整理的MFC版本链表演示程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。